2017-08-25 22 views
0

我正在嘗試使用MutationObserver監視對頁面的更新。我已經通讀了文檔(https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver & https://developers.google.com/web/updates/2012/02/Detect-DOM-changes-with-Mutation-Observers),但似乎無法得到這個權利。正確使用MutationObserver

我在做什麼錯?

var observer = new MutationObserver((mutations) => { 
    mutations.forEach((mutation) => { 
     console.log(mutation) 
    }) 
}) 

var config = {childList: true} 

observer.observe(document, config) 

回答

2

使用的DOM元素代替,

observer.observe(document.body, config)

0
// observer instance 
var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
    console.log(mutation); 

    // include your code for reacting to each mutation here 

    });  
}); 

// target node 
var target = document.getElementById('my-id'); 

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

// start observer 
observer.observe(target, config); 

// stop observer -- use this when you want to stop observing 
// observer.disconnect();