Home >Web Front-end >JS Tutorial >javascript copy node cloneNode() usage introduction_javascript skills

javascript copy node cloneNode() usage introduction_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:53:271701browse

The cloneNode(a) method accepts a Boolean parameter, indicating whether to perform a deep copy.
true: Indicates performing a deep copy, copying this node and the entire child node tree.
false: Shallow copy. Only the node itself is copied.
The node copy returned after copying belongs to the document, but does not have a parent node. Unless you use appendChild, insertChild(), replaceChild() to add it to the document

Copy code The code is as follows:


1
2< ;/span>
3

var oDiv = document.getElementById("guoDiv");
var deepList = oDiv.cloneNode(true ); //Copy child nodes
alert(deepList.childNodes.length); //3 or 7 (compatibility issue, so the results are different)
var showList = oDiv.cloneNode(false); //Only Copy the reference of the current element
alert(showList.childNodes.length); //0
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn