This article mainly introduces how to use the copy node cloneNode () for javascript node operations. If you need it, refer to the cloneNode (a) method to accept a Boolean parameter, indicating whether to perform deep copy.
True: Performs deep copy to copy the current node and the entire subnode tree.
False: copy the node only.
The node copy returned after the copy belongs to the document, but there is no parent node. Unless appendChild, insertChild (), replaceChild () is used to add it to the document
The Code is as follows:
1
2
3
Var oDiv = document. getElementById ("guoDiv ");
Var deepList = oDiv. cloneNode (true); // copy a subnode
Alert (deepList. childNodes. length); // 3 or 7 (the results are different due to compatibility issues)
Var showList = oDiv. cloneNode (false); // only copy the reference of the current element
Alert (showList. childNodes. length); // 0