[英]load innerHtml of ajax loaded div
我有一個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();
});
編輯:在op發布他的代碼之后
將委托綁定包裝在document.ready中就像這樣
$(function(){
$(document).delegate("#total", "keyup", function (e) {
if(e.which!=13){
var insidecontent=$('#topdiv').html();
}
});
});
$('#DivForContent').load('ajaxcontent.php #topdiv');
你需要確保在topdiv
附加到DOM后調用$('#topdiv')
。 load
根據http://api.jquery.com/load/進行回調
$('#someDiv').load('ajaxcontent.php', function() {
var insidecontent=$('#innerDiv').html();
//Do something with it.
});
既然你只展示了.load("ajaxcontent.php")
我假設你正在嘗試這樣做
jQuery("#foo").load("ajaxcontent.php")
var insidecontent=$('#topdiv').html()
您需要使用回調,因為加載是異步的。
function finishProcessing() {
var insidecontent=$('#topdiv').html();
alert( insidecontent );
}
jQuery("#foo").load("ajaxcontent.php", finishProcessing)
查看api for jQuery load
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.