2015-03-31 56 views

回答

2

根據http://en.wikipedia.org/wiki/DOM_events#Common.2FW3C_events DOM突變事件已被棄用,所以似乎沒有跨瀏覽器的方式來捕獲頁面內容更新上的事件。

phari在新的DOM突變事件API中發佈了link,這似乎在所有現代瀏覽器都支持。從該頁面用法示例代碼:

// select the target node 
var target = document.querySelector('#some-id'); 

// create an observer instance 
var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
    console.log(mutation.type); 
    });  
}); 

// configuration of the observer: 
var config = { attributes: true, childList: true, characterData: true }; 

// pass in the target node, as well as the observer options 
observer.observe(target, config); 

// later, you can stop observing 
observer.disconnect(); 

如果您需要對舊版瀏覽器的支持,你可以嘗試或許這jquery-observe具有回退舊版本瀏覽器。或者自己寫,就像這裏https://stackoverflow.com/a/14570614/4712360

另一種選擇是檢查網站的源代碼,也許你可以在網頁內容更新後找到一些事件發生在那裏。或者你可以掛鉤一些在內容更新後被調用的函數,或者,作爲最後的手段,你可以簡單地設置間隔函數來檢查頁面上的任何內容是否已經改變。

+1

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver – radiaph 2015-03-31 14:53:39