用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w
我試圖一些時(shí)間間隔后改變一個(gè)div的內(nèi)部HTML。 我得到我想要使用Ajax正確的反應(yīng)。 但無法取代內(nèi)HTML的后,并用Ajax響應(yīng)地選擇。 什么是錯(cuò)我的代碼..
HTML
51 seconds ago
58 seconds ago
.
.
.
.
.
10 minute ago
?查詢
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);
Answer 1:
this是在回調(diào)的窗口。 使用給予的價(jià)值callback每個(gè):
$( ".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);
}
});
});
你并不需要定義一個(gè)新的變量來保護(hù)this在jQuery已經(jīng)為您完成。
Answer 2:
當(dāng)您使用的是異步函數(shù)的回調(diào), this在你的回調(diào)并非來自同一背景。 您需要保存this在回調(diào)中使用的變量。
嘗試這樣的:
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);
Answer 3:
我認(rèn)為,$(這)是斷章取義。 嘗試:
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);
Answer 4:
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);
文章來源: Replace inner HTML of a div with Ajax response
總結(jié)
以上是生活随笔為你收集整理的用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codesys com库_CoDeSys
- 下一篇: 电脑怎么U盘装win10 U盘装Win1