5

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);
}

どちらもOnNodeInsertedChromeで呼び出します。バグですか?

4

2 に答える 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

重要なのはバブルです:はい-それがコンテナでも発射される理由です。

于 2011-11-26T10:42:12.557 に答える
-1

イベントがバブリングするのを防ぎたい場合は、event.stopPropagation();を使用してください。テキストノードのコールバックで。その後、イベントはDOMツリーで処理されなくなります。

于 2011-11-26T11:26:04.910 に答える