lab*_*ito 4 html jquery dom load
我想通过jQuery将另一个HTML(additional_content.html)中的表元素加载到当前的HTML中.我设法加载内容但我无法访问插入的元素,就好像它们没有插入一样.但是,在load语句之后插入alert语句时,我能够访问通过load插入的表的元素.在我看来,DOM树不会立即更新.
父文档中的代码片段如下所示:
<div id="content"></div>
<script>
$("#content").load("additonal_content.html #content table").hide();
$("#content").find("img").each(function() {
alert("test");
});
</script>
Run Code Online (Sandbox Code Playgroud)
*additional_content.html中的表格是(摘录):
<table>
<tr>
<td><img src="image1.gif"></td>
<td>some text...</td>
</tr>
<tr>
<td><img src="image2.gif"></td>
<td>some text...</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
小智 8
看起来内容在点击"查找"语句时尚未加载.
尝试使用回调,la:
<script>
$("#content").load("additonal_content.html #content table",function(){
$("#content").find("img").each(function() {
alert("test");
});
});
</script>
Run Code Online (Sandbox Code Playgroud)