When is the DOMNodeInserted event raised?

DOMNodeInserted event is triggered when node "is added to" or "is added"?

I ask about this because the following code:

function AddTextToContainer () {
    var textNode = document.createTextNode ("My text");
    var container = document.getElementById ("container");

    if (container.addEventListener) {
        container.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
    }
    container.appendChild (textNode);
}

So what:

function AddTextToContainer () {
   var textNode = document.createTextNode ("My text");
   var container = document.getElementById ("container");

   if (textNode.addEventListener) {
       textNode.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
   }
   container.appendChild (textNode);
}

Both are invoked OnNodeInsertedin Chrome. This is mistake?

+5
source share
2 answers

This is from W3C

DOMNodeInserted
Fired when a node has been added as a child of another node. 
This event is dispatched after the insertion has taken place. 
The target of this event is the node being inserted.
Bubbles: Yes
Cancelable: No
Context Info: relatedNode holds the parent node

The key is Bubbles: Yes , so it is also run in the container.

+9
source

, , event.stopPropagation(); node . dom.

-1

All Articles