php - 清除div内容

标签 php jquery mysql

$(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/

相关文章:

javascript - 使用 JavaScript 获取表单数据并使用 Ajax 发送数据

php - 查找数据库中与最多关键字匹配的记录

表或列上的 Mysql 授权

php - LIMIT 在页面中查看数据库中的所有数据

javascript - 根据classname查找TD中具有特定值的TR的数量

javascript - javascript中的多元素样式

mysql导入具有巨大blob表字段的大文件

php - 作为 Controller 的 Laravel 服务 - 使用多个 Controller

php - 这个sql语句怎么写

php - 使用 Doctrine 设置 "time"或 DateInterval