HTML DOM cloneNode()Method

元素对象参考手册Element Object

Examples

Copy a list item to another list:

var node=document.getElementById("myList2").lastChild.cloneNode(true);
document.getElementById("myList1").appendChild(node);

Before copy:

  • Coffee
  • Tea
  • Water
  • Milk

After copy:

  • Coffee
  • Tea
  • Milk
  • Water
  • Milk

Try it Yourself »

Definition and Usage

The cloneNode() method creates an exact copy of the specified node.

The cloneNode() method copies all attributes and their values.

This method duplicates and returns a copy of the node that calls it. If the parameter passed to it is true, it will also recursively copy all descendant nodes of the current node. Otherwise, it only copies the current node.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the cloneNode() method


Syntax

node.cloneNode(deep)

Parameters

Parameter Type Description
deep Boolean Optional. This method duplicates and returns a copy of the node that calls it. If the parameter passed to it is true, it will also recursively copy all descendant nodes of the current node. Otherwise, it only copies the current node.

Return Value

Type Description
Node Object The copied node

Technical Details

DOM Version Core Level 1 Node Object


元素对象参考手册Element Object

Other Extensions