日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w

發布時間:2025/3/8 HTML 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我試圖一些時間間隔后改變一個div的內部HTML。 我得到我想要使用Ajax正確的反應。 但無法取代內HTML的后,并用Ajax響應地選擇。 什么是錯我的代碼..

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是在回調的窗口。 使用給予的價值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已經為您完成。

Answer 2:

當您使用的是異步函數的回調, 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);

Answer 3:

我認為,$(這)是斷章取義。 嘗試:

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

總結

以上是生活随笔為你收集整理的用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。