[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
。这是一个错误吗?
这是来自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
键是气泡:是-这就是为什么还要在容器上发射它。
[如果要防止事件冒泡,请使用event.stopPropagation();在您的文本节点回调中。然后,事件不再在dom树上处理。