所以,我认为这会很简单,曾经有一个DOMNodeRemoved
事件,但不推荐使用,相反,应该使用MutationObserver ,问题是,即使配置适当,它也不会触发。
根据这篇关于从变异事件迁移到变异观察者的文章,检测节点的 dom 移除的{ childList: true, subtree: true }
配置childList
是到mdn 文章。subtree
无论如何,我已经解决了这个问题,它非常简单,<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);
});
谢谢!