addEventListener - detect changes in div-elements?

前端 未结 1 414
轮回少年
轮回少年 2021-01-06 10:25

I want to detect changes in div-elements. I tried already the \"addEventListener\" with some types (e.g.: change, load).

Here is my example, but the event won\'t tr

1条回答
  •  孤街浪徒
    2021-01-06 10:57

    You can use a MutationObserver.

    From MDN:

    // select the target node
    var target = document.querySelector('#some-id');
    
    // create an observer instance
    var observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {
        console.log(mutation.type);
      });    
    });
    
    // configuration of the observer:
    var config = { attributes: true, childList: true, characterData: true };
    
    // pass in the target node, as well as the observer options
    observer.observe(target, config);
    
    // later, you can stop observing
    observer.disconnect();
    

    Note: Deprecated

    What I have done in the extensions I wrote was to listen on DOMNodeInserted.

    div.addEventListener("DOMNodeInserted", function (e) {
        e.target // 
    }, false);
    

    0 讨论(0)
提交回复
热议问题