jquery 或 vanilla javascript 自动加载(jquery 加载)页面中的所有 link 内容
jquery or vanilla javascript auto load(jquery load) all link content in page
我看到了下载 links 的选项,但没有找到所有 links 的自动下载。
我需要自动加载,使用jquery加载或ajax或http请求或loadPageSection并不重要。任何选项都可以。
首先,我在每个 link.
上提出了带有点击模拟的最简单版本
$(function() {
document.querySelector('.className').click();
});
$("a").click(function() {
$("#pageLoad").load($(this).attr("href"));
return false;
});
但是,它起到了重定向的作用,重定向到第一页。
接下来,我替换了对每个 (function()
的点击,一切正常,尽管直到现在只加载了第一页
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function () {
$('a').each(function() {
$(".pageLoad").load($(this).attr('href') );
});
});
</script>
<table align="center">
<tr><td>
<a href="test.html" class="className">first</a>
<div class="pageLoad"></div></td><td>
<a href="test1.html" class="className">second</a>
<div class="pageLoad"></div></td><td>
<a href="test.html" class="className">third</a>
<div class="pageLoad"></div></td></tr>
</table>
必须加载所有页面。在每个 <a href
理想情况下,不应该有 <div class = "pageLoad">
并且所有内容都应该自动添加生成 div 并且可能作为子项添加。
最重要的是,jQuery 可以使用 load ('test.html #target');
下载所需的元素,现在该怎么做?
$(". pageLoad").load($ (this #target).attr ('href'));
不行吗?
在这种情况下如何下载所需的片段?
最后:如果加载的页面中也有link,会不会有问题?现在全部加载 1 次没有问题。
页面上的所有link只需要加载一次link的内容。
这些问题的解决方案对我来说太复杂了。
更新:
<script>
$.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/html; charset=windows-1251');
},
});
$(function () {
$('a').each(function() {
$(this).parent().find(".pageLoad").load($(this).attr('href') + ' #inf-1751');
});
});
</script>
我修复了编码的显示。
最后一个问题是在元素上使用锚点。如果你只添加
anchor,然后加载页面的副本,如果到另一个页面,anchor也不起作用,加载整个页面。
如果您想将每个页面加载到每个 link 相关容器 .pageLoad
,那么它可能看起来像那样。
$('a').each(function() {
$(this).parent().find(".pageLoad").load($(this).attr('href') );
});
如果您想即时添加.pageLoad
$('a').each(function() {
$(this).after('<div class="pageLoad"/>').load($(this).attr('href'));
});
无需手动添加.pageLoad
<table align="center">
<tr>
<td>
<a href="test.html" class="className">first</a>
</td>
<td>
<a href="test1.html" class="className">second</a>
</td>
<td>
<a href="test.html" class="className">third</a>
</td>
</tr>
</table>
我看到了下载 links 的选项,但没有找到所有 links 的自动下载。
我需要自动加载,使用jquery加载或ajax或http请求或loadPageSection并不重要。任何选项都可以。
首先,我在每个 link.
上提出了带有点击模拟的最简单版本$(function() {
document.querySelector('.className').click();
});
$("a").click(function() {
$("#pageLoad").load($(this).attr("href"));
return false;
});
但是,它起到了重定向的作用,重定向到第一页。
接下来,我替换了对每个 (function()
的点击,一切正常,尽管直到现在只加载了第一页
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function () {
$('a').each(function() {
$(".pageLoad").load($(this).attr('href') );
});
});
</script>
<table align="center">
<tr><td>
<a href="test.html" class="className">first</a>
<div class="pageLoad"></div></td><td>
<a href="test1.html" class="className">second</a>
<div class="pageLoad"></div></td><td>
<a href="test.html" class="className">third</a>
<div class="pageLoad"></div></td></tr>
</table>
必须加载所有页面。在每个 <a href
理想情况下,不应该有 <div class = "pageLoad">
并且所有内容都应该自动添加生成 div 并且可能作为子项添加。
最重要的是,jQuery 可以使用 load ('test.html #target');
下载所需的元素,现在该怎么做?
$(". pageLoad").load($ (this #target).attr ('href'));
不行吗?
在这种情况下如何下载所需的片段?
最后:如果加载的页面中也有link,会不会有问题?现在全部加载 1 次没有问题。
页面上的所有link只需要加载一次link的内容。
这些问题的解决方案对我来说太复杂了。
更新:
<script>
$.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/html; charset=windows-1251');
},
});
$(function () {
$('a').each(function() {
$(this).parent().find(".pageLoad").load($(this).attr('href') + ' #inf-1751');
});
});
</script>
我修复了编码的显示。 最后一个问题是在元素上使用锚点。如果你只添加 anchor,然后加载页面的副本,如果到另一个页面,anchor也不起作用,加载整个页面。
如果您想将每个页面加载到每个 link 相关容器 .pageLoad
,那么它可能看起来像那样。
$('a').each(function() {
$(this).parent().find(".pageLoad").load($(this).attr('href') );
});
如果您想即时添加.pageLoad
$('a').each(function() {
$(this).after('<div class="pageLoad"/>').load($(this).attr('href'));
});
无需手动添加.pageLoad
<table align="center">
<tr>
<td>
<a href="test.html" class="className">first</a>
</td>
<td>
<a href="test1.html" class="className">second</a>
</td>
<td>
<a href="test.html" class="className">third</a>
</td>
</tr>
</table>