我尝试使用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),但似乎无法做到这一点。
我做错了什么?
var observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
console.log(mutation)
})
})
var config = {childList: true}
observer.observe(document, config)
答案 0 :(得分:3)
使用DOM元素
observer.observe(document.body, config)
答案 1 :(得分:1)
// 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();