javascript检测何时删除某个元素

问题描述 投票:3回答:1

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

javascript tinymce
1个回答
2
投票

在大多数现代浏览器中,您可以使用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的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.