将当前HTML与PHP AJAX调用返回的数据进行比较


Compare current HTML with data returned from PHP AJAX call

我在比较页面上的当前列表项与通过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>

我不明白为什么字符串比较不起作用。我错过了一些东西,但我不确定是什么

试试这个:

<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>