Dom*_*nic 49

如果向下滚动一下,您会看到:

警告!该MutationEvent接口是在DOM Level 2 Events中引入的,但尚未在用户代理之间实现完全和互操作.此外,有人批评界面,如设计,引入了性能和实施挑战.正在开发一个新规范,目的是解决突变事件解决的用例,但是性能更高.因此,本规范描述了用于参考的遗传事件和遗留行为的完整性,但不赞成使用MutationEvent接口和MutationNameEvent 接口.

替换API是变异观察者,它在DOM Living Standard中完全指定,它取代了所有DOM级X的愚蠢.

  • http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers (6认同)
  • @ TJ - 没有失望.上面的那个是*DOMNodeRemovedFromDocument*.:-) (4认同)
  • 更换将在DOM Level 4 http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#mutation-observers中进行,看起来在Chromium中有一些进展https://bugs.webkit .ORG/show_bug.cgi?ID = 73851 (3认同)

ral*_*ise 21

我认为替换将是变异观察者:https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']};
var mutationObserver = new MutationObserver(function(mutationRecords) {
  $.each(mutationRecords, function(index, mutationRecord) {
    if (mutationRecord.type === 'childList') {
      if (mutationRecord.addedNodes.length > 0) {
        //DOM node added, do something
      }
      else if (mutationRecord.removedNodes.length > 0) {
        //DOM node removed, do something
      }
    }
    else if (mutationRecord.type === 'attributes') {
      if (mutationRecord.attributeName === 'class') {
        //class changed, do something
      }
    }
  });
});
mutationObserver.observe(document.body, whatToObserve);
Run Code Online (Sandbox Code Playgroud)