2011-10-26 43 views
0

如果旧内容和新内容不相同,我将如何去更新divs内容。只有在相同时才更新。如在,让我们说。目前我有这个:jQuery,加载前比较内容

<div style="min-height:200px;max-height:220px;height:auto;overflow-y:auto;padding:2px;" class="main"> 

<?php include("ajax/display_messages.php");?> 

</div> 

$(setInterval(function() { 

$('.main').load('ajax/display_messages.php', 

function(response, status, xhr){ }); 

}, 2000)); 

有没有什么办法让它首先检查内容?在更新之前?

回答

1

使用$.get()而不是.load()

var $main = $('.main'); 
$.get('ajax/display_messages.php', function (data) 
{ 
    if ($main.html() !== data) $main.html(data); 
}); 
+1

!===是这样吗? – Dev

+0

哎呀,谢谢。 –

+0

谢谢先生:-)。 –

0
$(setInterval(function() 
{ 
    $.get('ajax/display_messages.php',function(response) 
     { 
      if($('.main').html() != response) 
      { 
       $('.main').html(response); 
      } 
     }); 
}, 2000)); 
+0

不是。在HTML设置完成后,回调会触发。 –

+0

@Matt Ball:好的,你说得对,谢谢,我编辑了我的答案 –