2017-08-14 28 views
0

我目前有一個網頁,我正在使用.load()從另一個頁面加載div。在完成加載後,我運行一些jQuery代碼將特定的url附加到所有現有的hrefs。問題是,我第一次點擊一個超鏈接/ href我被重定向到舊的href。但是當我點擊瀏覽器上的「返回」按鈕後,我嘗試使用相同的超鏈接/ href,href工作並將我帶入新增的href。jquery .each()函數只對瀏覽器「回去」按鈕有效

我想看看是否有反正當我第一次加載頁面時,我所有的hrefs都會改變。

這裏是我的代碼:

我初始化,我會從第二頁容器裝載到容器中。

<pre> 
    <code id="iframe-fragment"></code> 
</pre> 

第二我第二頁的容器加載到我目前的網頁。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> 
<link href="https://roboticsys.com/support/plugins/servlet/docs/c1001-d1003/RapidCode/rsi_stylesheet.css" rel="stylesheet" type="text/css"> 
<script> 
    $('#iframe-fragment').load('https://roboticsys.com/support/plugins/servlet/docs/c1001-d1003/RapidCode/_absolute_motion_8cs-example.html div.fragment'); 
</script> 

我的URL追加到在導入容器中的所有的HREFs。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> 
<script> 
    $(document).ready(function() { 
    $("a").each(function() { 
     var $this = $(this);  
     var _href = $this.attr("href"); 
     $this.attr("href", 'https://appended-url.com/' + _href); 
    }); 
    }); 
</script> 

在此先感謝您。

回答

0

似乎您的load()內容正在被瀏覽器緩存,並且一旦您點擊後退按鈕,就會在頁面上顯示內容。

然而在此之前,在初始頁面加載時,您的文檔已完成,並運行您的每個文件,但加載不完整。在這種情況下,你的內容不會被更新。

我寧願通過負載complete回調來更新網址:

$('#div').load('http://myurl', function(res, status, xhr){ 
    //You can use the parameters for any sort of conditionals 
    $('a').each(function(){ 
     //your code to edit the URLs 
    }); 
}); 
+0

完美!有效!感謝您抽出時間。 –