Can I break with debugger on all changes to a DOM element?

无人久伴 提交于 2019-11-29 23:02:29

This is also doable without writing any script in Firebug as well as in Chrome's developer tools (maybe others, did not inspect further).

In Firebug:

  1. Go to HTML tab
  2. Right-click an element you'd like to monitor
  3. Choose "Break On Attribute Change", or "Break On Child Addition Or Removal", or "Break On Element Removal"

In Chrome Developer Tools

  1. Go to Elements tab
  2. Right-click an element you'd like to monitor
  3. Select "Break On ...", then choose "Subtree Modification", or "Attributes Modification", or "Node Removal"

I actually found this out after trying accepted answer of 999, however given code did not work for me. Additionally, Chrome's possibility to monitor events on any DOM subtree seems really nice.

James

Note: The events below were great when the question was asked, but are no longer current. The recommended alternative is MutationObservers, but those are still maturing

MutationObserver on MDN


Try this (in Firefox, with Firebug installed):

function breakOnChange(el) {

    if (!el.addEventListener) return;

    el.addEventListener('DOMAttrModified',
         function(DOMAttrModifiedEvent){debugger}, true);

    el.addEventListener('DOMNodeInserted',
         function(DOMNodeInsertedEvent){debugger}, true);

    el.addEventListener('DOMNodeRemoved',
         function(DOMNodeRemovedEvent){debugger}, true);

}

// Usage:
breakOnChange(someDomNode);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!