【问题标题】:Compare current HTML with data returned from PHP AJAX call将当前 HTML 与 PHP AJAX 调用返回的数据进行比较
【发布时间】:2013-01-08 20:51:40
【问题描述】:

我在将页面上的当前列表项与通过 PHP AJAX 刷新检索到的列表项进行比较时遇到问题。如果有新项目,我只希望该框刷新。

PHP 返回一堆格式化的 HTML 列表项。

<ul class="socialFeed twitterFeed">
    <script type="text/javascript">
        $(function() {
            refreshTweets();
        });
        function refreshTweets() {
            $.get("php/tweets.php", function(data){
                var current = $(".socialFeed.twitterFeed").html();
                if(current != data) {
                    $(".socialFeed.twitterFeed").hide().html(data).fadeIn();
                }
            });
        }
        setInterval("refreshTweets()", 2000);
    </script>
</ul>

我无法弄清楚为什么字符串比较不起作用。我错过了一些东西,但我不确定是什么。

【问题讨论】:

  • console_logdatacurrent 给你什么?您最好保存一个旧版本的data 进行比较。除此之外,我会尝试修改 php 以保存时间戳(例如在会话中),以便您可以使用它来仅获取自那时以来的推文。
  • 无法保证从 dom 生成的 html 与构建 dom 的 html 相同。
  • 穆萨,你能解释一下你的意思吗?
  • 您需要改变比较数据的方式。这些推文没有任何标识符吗?

标签: php jquery ajax string-comparison


【解决方案1】:

试试这个:

<ul class="socialFeed twitterFeed">
    <script type="text/javascript">
        var current;
        $(function() {
            refreshTweets();
        });
        function refreshTweets() {
            $.get("php/tweets.php", function(data){
                if(current != data) {
                    current = data;
                    $(".socialFeed.twitterFeed").hide().html(data).fadeIn();
                }
            });
        }
        setInterval(refreshTweets, 2000);
    </script>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多