$(function() {
setInterval(function() {
$.get("refresh.php", function(result) {
$('#response').empty()
.hide()
.html(result)
$("#response").append(result)
.fadeIn("slow");
});
}, 5000);
});
实际上,我的脚本在refresh.php中每5秒点击一次refresh.php,我从mysql数据库获取数据并创建一个模板,例如:
<table>
<tr><td>Name<td><td>$fetch['name']</td></tr>
</table>
我在 firebug 上检查了我的refresh.php 仅在 5 秒后发送响应一次,但在所有浏览器上它都会显示结果两次,如下所示:
<table>
<tr><td>Name<td><td>$fetch['name']</td></tr>
</table>
<table>
<tr><td>Name<td><td>$fetch['name']</td></tr>
</table>
最佳答案
它显示结果两次,因为您将其放在那里两次:首先使用 html
,然后使用 append
:
$(function() {
setInterval(function() {
$.get("refresh.php", function(result) {
$('#response').empty()
.hide()
.html(result) // <==== Here
$("#response").append(result) // <==== And here
.fadeIn("slow");
});
}, 5000);
});
html
用您提供的标记(或元素)替换元素的内容(在使用它之前不需要 empty
)。 append
追加您提供给元素的标记(或元素)。
你想要其中之一。我会选择 html
:
$(function() {
setInterval(function() {
$.get("refresh.php", function(result) {
$('#response')
.hide()
.html(result)
.fadeIn("slow");
});
}, 5000);
});
关于php - 清除div内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10036482/