2012-05-19 226 views
0

我正在爲Facebook的Chrome擴展工作。如果你使用Facebook,你知道當你向下滾動到新聞提要/時間軸/個人資料的底部時,它會顯示更多的帖子。該擴展實際上在「like」按鈕旁添加了一個按鈕。所以我需要檢查是否有更多的帖子添加該按鈕。檢查網頁是否已被修改

現在檢查頁面是否被修改,我使用setInterval(function(){},2000)。 我想在用戶點擊按鈕時運行一個函數。但是,如果我把它放在外面(甚至在裏面),這個函數不起作用setInterval() - 剛纔的Koder編輯

如何檢查網頁是否已經被修改而沒有使用循環?

例子:

$(document).ready(function(){ 
    window.setInterval(function(){ 
      $(".UIActionLinks").find(".dot").css('display','none'); 
      $(".UIActionLinks").find(".taheles_link").css('display','none'); 
      $(".miniActionList").find(".dot").css('display','none'); 
      $(".miniActionList").find(".taheles_link").css('display','none'); 
       //only this function doesn't work: 
      $(".taheles_link").click(function(){ 
      $(".taheles_default_message").hide(); 
      $(".taheles_saving_message").show(); 
      }); 
       //end 
      $(".like_link").after('<span class="dot"> · </span><button class="taheles_link stat_elem as_link" title="תגיד תכל&acute;ס" type="submit" name="taheles" onclick="apply_taheles()" data-ft="{&quot;tn&quot;:&quot;&gt;&quot;,&quot;type&quot;:22}"><span class="taheles_default_message">תכל&acute;ס</span><span class="taheles_saving_message">לא תכלס</span></button>'); 
      $(".taheles_saving_message").hide(); 
    }, 2000); 
}); 

在未來,這個擴展將使用AJAX,所以setInterval()甚至可以使更多的問題對我來說。

+0

你所說的 「希望在的setInterval()正在運行來運行更多功能」 是什麼意思?我不太明白你想要做什麼。也許一個例子會幫助我們理解 – koenpeters

+0

我想在用戶單擊按鈕時運行一個函數。但是,如果我把它放在外面(甚至是內部),這個函數不起作用setInterval() –

+0

所以,你的意思是你有一個點擊處理函數,你連接到按鈕的'click'事件,但是當你點擊它? – koenpeters

回答

0

如果我理解正確,您希望在頁面的DOM發生更改時收到通知。並且您希望在不使用setInterval()函數的情況下執行此操作。

由於您的問題在附加事件處理程序中,因此在頁面加載後創建的元素中,您可能有興趣檢查jquery.live事件附件技術。我認爲它會解決你的問題。

一般而言,您希望頁面引發突變事件。有一個mutation event spec,這可能是你在找什麼。以下是一些可能有用的鏈接。

  1. http://tobiasz123.wordpress.com/2009/01/19/utilizing-mutation-events-for-automatic-and-persistent-event-attaching/

  2. Detect element content changes with jQuery

+0

重要更新:使用突變事件的每個人現在應該切換到突變觀察者。請參閱[問題](http://stackoverflow.com/questions/11425209/are-dom-mutation-observers-slower-than-dom-mutation-events),[Mozilla參考](https://developer.mozilla.org/EN-US /文檔/ DOM/MutationObserver?redirectlocale = EN-US&redirectslug = DOM%2FDOM_Mutation_Observers) –

0
$(document).ready(function(){ 
    setInterval('fun()',5000); 
    fun(); 
}); 
function fun() 
{ 
    alert(11) 
}