Node.cloneNode()
method returns a duplicate of the node on which this method was called.
let newClone = node.cloneNode([deep])
node
The node to be cloned.
newClone
The new node, cloned from
node
.
newClone
has no parent and is not part of the document,
直到
it is added to another node that is part of the document (using
Node.appendChild()
or a similar method).
deep
可选
*
若
true
,那么
node
and its whole subtree—including text that may be in child
文本
nodes—is also copied.
若
false
, only
node
will be cloned. Any text that
node
contains is not cloned, either (since text is contained by one or more child
文本
nodes).
deep
has no effect on empty elements (such as the
<img>
and
<input>
元素)。
*
注意:
In the DOM4 specification (since Gecko 13.0 (Firefox 13 / Thunderbird 13 / SeaMonkey 2.10)), the optional
deep
自变量默认为
true
.
This behavior has been changed in the latest spec!
尽管
deep
it still optional, it now defaults to
false
.
You should always provide an explicit value for backward and forward compatibility.
let p = document.getElementById("para1")
let p_prime = p.cloneNode(true)
Cloning a node copies all of its attributes and their values, including intrinsic (inline) listeners. It does
not
copy event listeners added using
addEventListener()
or those assigned to element properties (e.g.,
node
.onclick =
someFunction
). Additionally, for a
<canvas>
element, the painted image is not copied.
警告:
cloneNode()
may lead to duplicate element IDs in a document!
If the original node has an
id attribute
, and the clone will be placed in the same document, then you should modify the clone's ID to be unique.
名称
attributes may need to be modified also, depending on whether duplicate names are expected.
To clone a node to insert into a
different
document, use
Document.importNode()
代替。
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
cloneNode
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 7 | Safari 1.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
deep
默认为
false
|
Chrome Yes | Edge 12 |
Firefox
29
|
IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes |
Firefox Android
29
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
见实现注意事项。
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument