Introduction to the use of javascript copy node cloneNode of

  • 2020-03-30 02:30:36
  • OfStack

The cloneNode(a) method takes a Boolean parameter to indicate whether or not to copy deeply
True: performs a deep copy, copying the local node and the entire child tree.
False: shallow copy. Just copy the node itself.
The copy of the node returned after the copy is owned by the document, but there is no parent, unless appendChild,insertChild(), and replaceChild() are added to the document
 
<div id="guoDiv"> 
<span>1</span> 
<span>2</span> 
<span>3</span> 
</div> 
var oDiv = document.getElementById("guoDiv"); 
var deepList = oDiv.cloneNode(true); //Copy child node
alert(deepList.childNodes.length); //3 or 7(compatibility issues, so results are different)
var showList = oDiv.cloneNode(false); //Copy only references to the current element
alert(showList.childNodes.length); //0 

Related articles: