欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > HTML >内容正文

HTML

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

发布时间:2025/3/8 HTML 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 用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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。