Copy a <li> element from "myList2" to "myList1":
const node = document.getElementById("myList2").lastChild;
const clone = node.cloneNode(true);
document.getElementById("myList1").appendChild(clone);
Before:
After:
More examples below.
The cloneNode()
method creates a copy of a node, and returns the clone.
The cloneNode()
method clones all attributes and their values.
Set the deep parameter to true
if you also want to clone descendants (children).
To insert a cloned node back into the document, use:
node.cloneNode(
deep)
Parameter | Description |
deep | Optional.false - Default. Clone only the node and its attributes.true - Clone the node, its attributes, and its descendants. |
Type | Description |
Node | The cloned node. |
Copy the "demo" element, including attributes and child elements, and append it to the document:
const node = document.getElementById("demo");
const clone = node.cloneNode(true);
document.body.appendChild(clone);
Try it Yourself »
element.cloneNode()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!