【发布时间】:2014-12-13 05:00:15
【问题描述】:
在从元素中正确获取操作数据时遇到一些问题。 互联网上的任何地方似乎都没有用简单的答案来涵盖如此简单的问题。请帮忙。
我使用返回的 ajax 请求操作了一个元素:
$("#last_comment_added").html("1457856458")
现在我在页面上的功能:
jQuery(document).ready(function($) {
var post_slug = $("#post_slug").html();
var last_comment_added = $("#last_comment_added").text();
if (post_slug && last_comment_added) {
setInterval(function() {
$.ajax({
type: "POST",
url: "ajax_comments.php",
data: {
"task": "updates",
"post_slug": post_slug,
"last_comment_added": last_comment_added
},
cache: false,
success: function(html) {
eval(html)
}
});
}, 10000);
}
});
我从元素中获取旧数据,而不是新的 ajax“1457856458”数据。
请帮忙。
【问题讨论】:
-
控制台有错误吗?为什么
eval(html)挂在那里?将其记录到控制台以查看您是否获得了正确的结果。 -
没有错误。我的代码工作正常。问题是它返回旧数据(页面最初加载时)而不是新数据(在 ajax 请求之后)。我怀疑这是 DOM 没有更新。我想知道是否有办法更新 DOM。
-
当然,只需将要更新的整个 html 放在一个 div 中,然后使用
.html()函数将特定 div 的内容更改为success函数中的新内容。
标签: javascript jquery html ajax dom