javascript 检测某个元素何时被删除

javascript detect when a certain element is removed

javascript 中是否有方法检测 javascript/jQuery 中某个 HTML 元素何时被删除? 在 TinyMCE 中,我插入了一个滑块,当在 WYSIWIG 中删除某些元素时,我想删除整个东西。

在大多数现代浏览器中,您可以使用 MutationObserver 来实现这一点。

你会这样做的方式是这样的:

var parent = document.getElementById('parent');
var son = document.getElementById('son');

console.log(parent);
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation); // check if your son is the one removed
  });
});

// configuration of the observer:
var config = {
  childList: true
};

observer.observe(parent, config);

son.remove();

您可以检查一个 运行 示例 here

还有关于 MutaitionObserver 的更多信息 here