何时调用“ DOMNodeInserted”事件?

问题描述 投票:5回答:2

[DOMNodeInserted事件在节点“附加到”或“附加”时被调用?

我问这个是因为以下代码:

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

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

和那个:

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在Chrome中调用OnNodeInserted。这是一个错误吗?

javascript dom dom-events dom-node
2个回答
9
投票

这是来自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

键是气泡:是-这就是为什么还要在容器上发射它。


-1
投票

[如果要防止事件冒泡,请使用event.stopPropagation();在您的文本节点回调中。然后,事件不再在dom树上处理。

© www.soinside.com 2019 - 2024. All rights reserved.