【发布时间】:2015-04-20 07:45:42
【问题描述】:
所以,我认为这会很简单,曾经有一个 DOMNodeRemoved 事件,但它已被弃用,而是应该使用 MutationObserver,问题是,它不会触发,甚至适当的配置。
根据this article关于从变异事件迁移到变异观察者的配置,检测节点的dom删除的配置是{ childList: true, subtree: true },这符合childList是强制性的,subtree意味着它将捕获根据the mdn article,不仅要观察目标的突变,还要观察目标的后代。
无论如何,我已经提出了问题的jsfiddle,它非常简单,<button> 删除了<div>,观察者应该记录突变记录,但它没有,看看你是否可以想办法:)
HTML
<div>Oh my god karen, you can't just ask someone why they're white.</div>
<button>^Remove</button>
JavaScript
div = document.querySelector("div");
callback = function(records){
console.log(records);
}
config = {
childList:true,
subtree:true
}
observer = new MutationObserver(callback);
observer.observe(div,config);
button = document.querySelector("button");
button.addEventListener("click",function(){
div.parentNode.removeChild(div);
});
谢谢!
【问题讨论】:
-
我喜欢 DOMNodeRemoved ) 看来我们应该更喜欢每次都使用 IE )))
标签: javascript dom mutation-observers mutation-events