我正在尝试在一段时间后更改 div 的内部 HTML。 我得到了 Ajax 想要的正确响应。 但无法用 Ajax 响应替换选定的内部 HTML。 我的代码有什么问题..
HTML
<p class="time ui-li-desc" data-time="2013-02-13 11:30:08" >
51 seconds ago<img alt="image" src="images/read.png"></p>
<p class="time ui-li-desc" data-time="2013-02-13 11:30:16" >
58 seconds ago<img alt="image" src="images/read.png"></p>
.
.
.
.
.
<p class="time ui-li-desc" data-time="2013-02-13 11:40:08" >
10 minute ago<img alt="image" src="images/read.png"></p>
j查询
setInterval(function() {
$( ".time" ).each(function( index ) {
var sendTime= $(this).attr("data-time");
dataString = "sendtime="+sendTime+"&q=convertTime";
$.ajax({
type: "POST",
url: "data_handler.php",
data: dataString,
cache: true,
success: function(response) {
alert(response);
$(this).html(response);
//alert(response);
}
});
});
}, 5000);
最佳答案
this
是回调中的窗口。使用赋予每个 :
callback
的值
$( ".time" ).each(function(index , elem) {
var sendTime= $(this).attr("data-time");
dataString = "sendtime="+sendTime+"&q=convertTime";
$.ajax({
type: "POST",
url: "data_handler.php",
data: dataString,
cache: true,
success: function(response) {
alert(response);
$(elem).html(response);
}
});
});
您不需要定义一个新变量来保护this
,因为jQuery 已经为您做了。
关于javascript - 用 Ajax 响应替换 div 的内部 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851684/