Puz*_*Boy 6 html javascript ajax jquery
我试图在一段时间后改变div的内部HTML.我得到了正确的回应,我想用Ajax.但无法替换后选择的内部HTML和Ajax响应.我的代码有什么问题..
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>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
你并不需要定义一个新的变量,以保护this在jQuery已经为您完成.
由于您将异步函数与回调一起使用,因此this您的回调并非来自同一上下文。您需要保存this在回调中使用的变量。
尝试这样:
setInterval(function() {
$( ".time" ).each(function( index ) {
var sendTime= $(this).attr("data-time");
dataString = "sendtime="+sendTime+"&q=convertTime";
var self = this;
$.ajax({
type: "POST",
url: "data_handler.php",
data: dataString,
cache: true,
success: function(response) {
alert(response);
$(self).html(response);
//alert(response);
}
});
});
}, 5000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36918 次 |
| 最近记录: |