1
我只是試着用jQuery的.load方法改變我的頁面內容。爲什麼jquery在.load()後面看到頁面的變化?
雖然內容變化沒有問題,我發現jQuery的仍然「看到」舊的內容,如果我試圖選擇的東西,一個例子:
頁:
<div id="mycontentarea">
<div id="myfirstcontent"></div>
<div id="mysecondcontent"></div>
</div>
更換內容:
<div id=mythirdcontent"></div>
<div id=myfourthcontent"></div>
的javascript:
// replace original content
$('#mycontentarea').load('replacement.html');
// print out the id of the first child
console.log($("#mycontentarea").children().attr("id"));
控制檯將打印出「myfirstcontent」而不是「mythirdcontent」 - 爲什麼?
如果您使用'load'來動態地將事件重新附加到動態內容,則需要使用jQueries'live'或'delegate'。 – ppumkin 2012-04-27 10:45:14