2015-06-01 66 views
0
$.ajax({ 
    url: "/path/to/dynamic/html", 
    type : "GET" 
}).done(function(result){ 
    $("#content").html(result) 
    setTimeout(function(){ 
    console.log("Now, the html content is " + document.body.innerHTML)}, 10000) 
}) 

/path/to/dynamic/html URL返回一个纯字符串作为, i am the dynamic content文献innerHTML的显示旧的内容,对AJAX负载

10秒的届满后,我预期控制台打印,

<div id="content">i am the dynamic content</div> 

但仍然,控制台打印旧内容为,

<div id="content">i am the static content</div> 

但是页面会以新内容呈现。

为什么console.log即使设置了十秒的失效后也显示旧的DOM内容?

+0

u能试试吗? $ .get(url,function(data){('#content')。html(data); }); – Sushil

+0

上述代码在给定的形式工作...我在原始代码中将错误的函数参数传递给'setTimeout'。 –

回答

0

既然你的jQuery,使用jQuery的方法:

$("body").html() 

当你没有提供参数给.html()方法,它返回的HTML。当你提供一个参数时,它会替换HTML。

关于该方法的更多信息,请参考.html() method in the jQuery docs


编辑:

因此,要将此到您的代码,你会做这样的事情:

$.ajax({ 
    url: "/path/to/dynamic/html", 
    type : "GET" 
}).done(function(result){ 
    $("#content").html(result) 
    setTimeout(function(){ 
    console.log("Now, the html content is " + $("body").html())}, 10000) 
}) 
+0

嗨Jamen,我试过它,但'$(“#content”)。html()'返回旧内容.... –

+0

@SaravanaKumar,这很奇怪。你确定HTML开始改变吗? –

+0

Jamen,你是对的......我在传递'setTimeout'函数参数时犯了一个错误。而不是把值作为第三个参数,我给了'func_arg(arg1)'。但是,在试图简化错误时,我忘记了抓住这一点......谢谢...... –

相关问题