Detect changes in the DOM

后端 未结 7 2320
温柔的废话
温柔的废话 2020-11-21 05:45

I want to execute a function when some div or input are added to the html. Is this possible?

For example, a text input is added, then the function should be called.<

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 06:03

    Ultimate approach so far, with smallest code:

    (IE9+, FF, Webkit)

    Using MutationObserver and falling back to the deprecated Mutation events if needed:
    (Example below if only for DOM changes concerning nodes appended or removed)

    var observeDOM = (function(){
      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
    
      return function( obj, callback ){
        if( !obj || obj.nodeType !== 1 ) return; 
    
        if( MutationObserver ){
          // define a new observer
          var mutationObserver = new MutationObserver(callback)
    
          // have the observer observe foo for changes in children
          mutationObserver.observe( obj, { childList:true, subtree:true })
          return mutationObserver
        }
        
        // browser support fallback
        else if( window.addEventListener ){
          obj.addEventListener('DOMNodeInserted', callback, false)
          obj.addEventListener('DOMNodeRemoved', callback, false)
        }
      }
    })()
    
    
    //------------< DEMO BELOW >----------------
    
    // add item
    var itemHTML = "
  • ", listElm = document.querySelector('ol'); document.querySelector('body > button').onclick = function(e){ listElm.insertAdjacentHTML("beforeend", itemHTML); } // delete item listElm.onclick = function(e){ if( e.target.nodeName == "BUTTON" ) e.target.parentNode.parentNode.removeChild(e.target.parentNode); } // Observe a specific DOM element: observeDOM( listElm, function(m){ var addedNodes = [], removedNodes = []; m.forEach(record => record.addedNodes.length & addedNodes.push(...record.addedNodes)) m.forEach(record => record.removedNodes.length & removedNodes.push(...record.removedNodes)) console.clear(); console.log('Added:', addedNodes, 'Removed:', removedNodes); }); // Insert 3 DOM nodes at once after 3 seconds setTimeout(function(){ listElm.removeChild(listElm.lastElementChild); listElm.insertAdjacentHTML("beforeend", Array(4).join(itemHTML)); }, 3000);
    
    
    1. …More will be added after 3 seconds…

提交回复
热议问题