2011-10-26 37 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:好的,你說得對,謝謝,我編輯了我的答案 –