HTML响应与旧的HTML相比

时间:2010-05-22 21:28:17

标签: jquery ajax

我有一个AJAX请求的响应,使用jQuery作为'response'返回

我需要将它与旧HTML进行比较。如果它不同,它只会做一些css。

问题是它每次都会检测到不同的变化,即使没有任何差异,并且有一个连续的循环使我的动画在css上重复。

var oldHTML = $("#container").html();
$.ajax({  
        url: "ajax/log.php",  
        cache: false,
        dataType: "JSON",
        success: function(response){
            if(oldHTML != response)
            {
                $("#container").css({"border":"1px solid red"}),
                $("#container").animate({"border":"none"}); 
            }
        }

它并不总是检测到变化或没有变化。它还会循环动画位。

2 个答案:

答案 0 :(得分:1)

当html被附加到DOM时,可能会对其进行更改。我会将HTML作为数据对象存储到元素中,以保留它的副本。

   // get the saved response, if any, that was saved on the last ajax request
    var oldHTML = $("#container").data('oldHTML');
    $.ajax({  
                    url: "ajax/log.php",  
                    cache: false,
                    dataType: "JSON",
                    success: function(response){
                      if(oldHTML != response){
                         $("#container").css({"border":"1px solid red"});
                         $("#container").animate({"border":"none"}); }
                        // assign the original server response to oldHTML
                         $("#container").data('oldHTML', response);
                        // shove response into the DOM
                         $("#container").html(response);
                       }else {
                          // i guess you're doing nothing here?
                      }
           });

或类似的,希望传达这个想法

答案 1 :(得分:0)

我会改写你的问题,提到jQuery& AJAX - 花了我一秒钟才看到标签。

我会使用文件比较,例如WinMerge(免费),看看有什么区别。

我的猜测是它可能与间距有关,在这种情况下你可以使用修剪功能。

另外,如果您发布代码,那将会有所帮助。