DOM Mutation event in JQuery or vanilla Javascript

前端 未结 6 1037
独厮守ぢ
独厮守ぢ 2020-11-27 04:21

Are there any DOM mutation events in JQuery or in vanilla Javascript that fire cross browser?

To clarify, say I have a script on my page which inserts a div into the

相关标签:
6条回答
  • 2020-11-27 04:37

    There are some deprecated DOM mutation events, such as DOMNodeInserted and DOMNodeInsertedIntoDocument that still work for me in Chrome 14 and IE9.

    See an example at http://jsfiddle.net/Kai/WTJq6/

    See them all on the W3C draft at http://www.w3.org/TR/DOM-Level-3-Events/

    0 讨论(0)
  • 2020-11-27 04:40

    JCADE (JQuery Create and Destroy Events) will do this. It uses behaviors for IE and DOM mutation events for other browsers. It does not use timing events and does not wrap JQuery methods like livequery.

    https://github.com/snesin/jcade

    
        $( document ).create( "a", function( event ) {
          alert( event.target );
        });
    
    
    0 讨论(0)
  • 2020-11-27 04:50

    If you are looking for an alternative to this method, here is http://www.jqui.net/jquery-projects/jquery-mutate-official/ which can also allow you to add events yourself and keep a watch for almost any change, class name change, height change, width change.

    0 讨论(0)
  • 2020-11-27 04:50

    The livequery plugin can be used for this. In version 1.x a timer was used to periodically check for any changes. However, the newer version 2.x branch seems to be working without periodical timeout (untested).

    0 讨论(0)
  • 2020-11-27 05:01

    This is certainly a hack, but why not patch the underlying DOM methods used to insert the nodes? There are a couple ways to do this:

    A. You know what specific element will be appended to:

    var c = document.getElementById('#container');
    c.__appendChild = c.appendChild;
    c.appendChild = function(){
         alert('new item added');
         c.__appendChild.apply(c, arguments); 
    }
    

    fiddle demo for A

    B. You know what type of element will be appended to:

    HTMLDivElement.prototype.__appendChild = HTMLDivElement.prototype.appendChild;
    HTMLDivElement.prototype.appendChild = function(){
        alert('new item added');
        HTMLDivElement.prototype.__appendChild(this,arguments); 
    }
    

    fiddle demo for B

    (Note that solution B is not supported by IE < 8 or any other browser which does not support DOM prototypes.)

    This same technique could just as easily be used on all the underlying DOM mutation functions such as insertBefore, replaceChild or removeChild.

    That's the general idea, these demos could be adapted for pretty much any other use case -- say you want to cast a wide net and catch all additions regardless of type AND make sure it works across all browsers everything but IE < 8? (see example C below)


    UPDATE

    C. Recursively walk the DOM, swap out the function on every element to trigger a callback, and then apply the same patch to any children being appended.

    var DOMwatcher = function(root, callback){
      var __appendChild = document.body.appendChild;
    
      var patch = function(node){
        if(typeof node.appendChild !== 'undefined' && node.nodeName !== '#text'){
          node.appendChild = function(incomingNode){
            callback(node, incomingNode);
            patch(incomingNode);
            walk(incomingNode);
            __appendChild.call(node, incomingNode);
          };
        }
        walk(node);  
      };
    
      var walk = function(node){
        var i = node.childNodes.length;
        while(i--){
          patch(node.childNodes[i]);
        }
      };
    
      patch(root);
    
    };
    
    DOMwatcher(document.body, function(targetElement, newElement){ 
       alert('append detected');    
    });
    
    $('#container ul li').first().append('<div><p>hi</p></div>');
    $('#container ul li div p').append('<a href="#foo">bar</a>');
    

    fiddle demo for C

    UPDATE 2

    As Tim Down commented, the above solution also won't work in IE < 8 because appendChild is not a Function and does not support call or apply. I suppose you could always fall back to the clunky but trusty setInterval method if typeof document.body.appendChild !== 'function'.

    0 讨论(0)
  • 2020-11-27 05:03

    Not without a plugin, I believe, but I wouldn't be surprised if I'm wrong.

    My research has found a a few to choose from.

    Here's one: http://plugins.jquery.com/project/mutation-events

    Here's another: https://www.adaptavist.com/display/jQuery/Mutation+Events

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