Can javascript listen for “onDomChange” on every Dom elements?

前端 未结 2 1373
梦如初夏
梦如初夏 2020-12-06 06:42

My question is somewhat weird, but can i add events for any DOM elements(div), like \'onHtmlChange\', to be notified when that div has changed their content?

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 07:30

    The DOMNodeInserted and DOMNodeRemoved are part of the Mutation Events which is now deprecated. Hence, you need to take a look on Mutation Observer. I did the same in one of my project which is I needed to listen to the DOM changes of a certain element., e.g. DOM append, remove, attribute, and data changes.

    These links will help you :

    1. http://gabrieleromanato.name/jquery-detecting-new-elements-with-the-mutationobserver-object/
    2. https://github.com/kapetan/jquery-observe
    3. Is there a JavaScript/jQuery DOM change listener?

提交回复
热议问题