[英]How to know if new element is add to the DOM
我讀到了 MutationObserver function 或者我認為它是一個接口但我不能使用它誰能告訴我?
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log("new element has been added");
console.log(mutation.target.nodeName);
});
});
當然, MutationObserver
是一個接口,但您需要調用observe()
function 來檢測 DOM 中的任何變化:
observer.observe(targetNode, observerOptions);
您可以嘗試將此添加到您的代碼中:
observer.observe(document, {attributes: true, childList: true, characterData: false, subtree:true});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.