我有一个 div,它通过 ajax .load("ajaxcontent.php") 加载它的内容。
在这个 div 中,我们产生了许多其他的 div。我想获取其中一个 div 的内容。
我试过 var insidecontent=$('#topdiv').html();但它返回 null。
获取这样的值有技巧吗?
index.html
<div id="news"></div>
ajaxcontent.php
<div id="topdiv">Hello World</div>
<div id="middiv">This is the mid div</div>
<div id="botdiv">This is the bottom</div>
JavaScript:
$("#news").load("ajaxcontent.php");
$(document).delegate("#total", "keyup", function (e) {
if(e.which!=13){
var insidecontent=$('#topdiv').html();
}
});
最佳答案
您是否在加载函数完成执行后访问内容?
$("#firstDiv").load("serverpage.aspx",function(){
// Now access the content
var insideContent=$("#topDiv").html();
});
编辑:运算符(operator)发布他的代码后
像这样将委托(delegate)绑定(bind)包装在 document.ready 中
$(function(){
$(document).delegate("#total", "keyup", function (e) {
if(e.which!=13){
var insidecontent=$('#topdiv').html();
}
});
});
关于javascript - 加载 ajax 加载的 div 的 innerHtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625317/