簡體   English   中英

jQuery或Vanilla JavaScript自動加載(jQuery加載)頁面中的所有鏈接內容

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

我看到了下載鏈接的選項,但沒有找到自動下載所有鏈接的選項。

我需要自動加載,使用jquery加載或ajax或http請求或loadPageSection並不重要。 任何選擇都可以。

首先,我想出了每個鏈接上帶有點擊模擬功能的最簡單版本。

$(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');成為可能load ('test.html #target'); 下載所需的元素,現在該怎么做?

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

在這種情況下,如何下載所需的片段?

最后:如果加載的頁面中也有鏈接,會不會有問題? 現在,盡管全部加載1次,但沒有問題。

對於頁面上的所有鏈接,只需要加載一次鏈接的內容。

這些問題的解決方案對我來說太復雜了。

更新:

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

我固定了編碼的顯示。 最后一個問題是在元素上使用錨。 如果僅添加錨點,則加載頁面的副本;如果添加到另一個頁面,則錨點也將不起作用,從而加載整個頁面。

如果要將每個頁面加載到與鏈接相關的容器.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM