简体   繁体   English

jQuery或Vanilla JavaScript自动加载(jQuery加载)页面中的所有链接内容

[英]jquery or vanilla javascript auto load(jquery load) all link content in page

I saw the options for downloading links, but did not find there the automatic download of all links. 我看到了下载链接的选项,但没有找到自动下载所有链接的选项。

I need automatic loading, it's not important to use jquery load or ajax or http request or loadPageSection. 我需要自动加载,使用jquery加载或ajax或http请求或loadPageSection并不重要。 Any option is fine. 任何选择都可以。

First I came up with the simplest version with the click emulation, on each link. 首先,我想出了每个链接上带有点击模拟功能的最简单版本。

$(function() {
  document.querySelector('.className').click();
});

$("a").click(function() {
  $("#pageLoad").load($(this).attr("href"));
  return false;
});

However, it worked as a redirect, redirecting to the first page. 但是,它用作重定向,重定向到第一页。 Next, I replaced the click on each (function() , everything works although until now only the first page is loaded 接下来,我替换了每个(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>

It is necessary that all pages are loaded. 必须加载所有页面。 under each <a href Ideally, there should not be a <div class = "pageLoad"> and everything should be added automatically generating a div and perhaps added as a child. 在每个<a href下。理想情况下,不应有<div class = "pageLoad">并且应自动添加所有内容以生成div并可能作为子级添加。

And most importantly, jQuery makes it possible to use load ('test.html #target'); 最重要的是,jQuery使使用load ('test.html #target');成为可能load ('test.html #target'); to download the required element, how to do it now? 下载所需的元素,现在该怎么做?

$(". pageLoad").load($ (this #target).attr ('href')); does not work? 不起作用?

How to download the required fragment in this case? 在这种情况下,如何下载所需的片段?

And the last: if there are also links in the loaded page, will there be any problems? 最后:如果加载的页面中也有链接,会不会有问题? Now while all is loaded 1 time there are no problems. 现在,尽管全部加载1次,但没有问题。

It is necessary to load the contents of links only once for all links on the page. 对于页面上的所有链接,只需要加载一次链接的内容。

The solution to these problems is too complex for me. 这些问题的解决方案对我来说太复杂了。

update: 更新:

<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>

I fixed the display of the encodings. 我固定了编码的显示。 The last problem is to use the anchors on the elements. 最后一个问题是在元素上使用锚。 If you only add anchor, then load a copy of the page, if to another page, the anchor also does not work, loading the entire page. 如果仅添加锚点,则加载页面的副本;如果添加到另一个页面,则锚点也将不起作用,从而加载整个页面。

If you want to load each page into each your link related container .pageLoad , then it might look like that. 如果要将每个页面加载到与链接相关的容器.pageLoad ,则可能看起来像这样。

$('a').each(function() {
   $(this).parent().find(".pageLoad").load($(this).attr('href') );
});

If you would like to add .pageLoad on fly 如果您想动态添加.pageLoad

$('a').each(function() {
   $(this).after('<div class="pageLoad"/>').load($(this).attr('href'));
});

No need to add .pageLoad manually 无需手动添加.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM