Javascript 用 Ajax 响应替换 div 的内部 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14851684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace inner HTML of a div with Ajax response
提问by Puzzled Boy
i am trying to change inner HTML of a div after some interval. i am getting right response which i want with Ajax. but unable to replace inner HTML of selected after and with Ajax response. what is wrong with my code..
我试图在一段时间后更改 div 的内部 HTML。我得到了我想要的 Ajax 正确响应。但无法用 Ajax 响应替换选定的内部 HTML。我的代码有什么问题..
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 query
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);
回答by Denys Séguret
thisis the window in the callback. Use the value given to the callbackof each :
this是回调中的窗口。使用给定给callbackeach的值:
$( ".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);
}
});
});
You don'tneed to define a new variable to protect thisas jQuery already does it for you.
你并不需要定义一个新的变量,以保护this在jQuery已经为您完成。
回答by sdespont
As you are using an asynchronous function with a callback, thisin your callback doesn't come from the same context. You need to save thisin a variable used in the callback.
当您使用带有回调的异步函数时,this在您的回调中不会来自相同的上下文。您需要保存this在回调中使用的变量中。
Try like 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);
回答by Yevgen
I think $(this) is out of context. Try:
我认为 $(this) 是断章取义。尝试:
setInterval(function() {
$( ".time" ).each(function( index ) {
var $this = $(this);
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);
回答by Dineshkani
setInterval(function() {
$( ".time" ).each(function( index ) {
var sendTime= $(this).attr("data-time");
var _thisvariable = $(this);
dataString = "sendtime="+sendTime+"&q=convertTime";
$.ajax({
type: "POST",
url: "data_handler.php",
data: dataString,
cache: true,
success: function(response) {
alert(response);
_thisvariable.html(response);
//alert(response);
}
});
});
}, 5000);

