{{翻译}}Node.cloneNode

来源:互联网 发布:人工智能开发语言最好 编辑:程序博客网 时间:2024/05/16 13:48
{{翻译}}Node.cloneNode

返回Node结点的副本。

调用方法:
var dupNode = node.cloneNode(deep);
其中参数
node:原结点
dupNode:原结点的副本
deep:
    true 原结点的子结点将会被cloned
    false 只clone指定的结点

Note: In the DOM4 specification (as implemented in Gecko 13.0 (Firefox 13.0 / Thunderbird 13.0) ), deep is an optional argument. If omitted, the method acts as if the value of deep was true, defaulting to using deep cloning as the default behavior. If you want to use shallow cloning, you need to explicitly set this argument to false.

In browsers supporting previous versions of the DOM, you always need to provide this argument.

用例:
var p = document.getElementById("para1"),
    p_prime = p.cloneNode(true);

注意:
拷贝结点会拷贝所有的属性和属性值,但不会拷贝结点绑定的事件监听器。

cloneNode()返回的结点副本直到使用Node.appendChild()或其它的方法添加到文档中的某个结点时才会成为文档的一部分。在此之前该结点副本不具有父结点。
若deep置为false,则不会有子结点被拷贝。该结点中所含的任意文本都不会被拷贝。
若deep置为true,则整个子结点树都会被拷贝。

Warning: cloneNode() may lead to duplicate element IDs in a document.(cloneNode()可能导致文档中出现重复的元素id)

将由cloneNode()获得的结点副本添加到其它结点中时它会获得新的uniqueID.
The duplicate node returned by cloneNode() receives a new uniqueID when it is added to another node

若想拷贝结点添加到其它的document中,请使用Document.importNode().
To clone a node for appending to a different document, use Document.importNode() instead.

原文:https://developer.mozilla.org/en/DOM/Node.cloneNode
原创粉丝点击