Home > Article > Web Front-end > JavaScript code for cloning nodes using cloneNode method_javascript skills
Many times we use for to generate multiple node structures with the same structure, so we need to write a lot of createElement, setAttribute, appendChild and other codes.
But in fact, we only need an html template, and we can use the cloneNode method to clone existing nodes, including their child nodes.
The following is the cloneNode method prototype:
newElement oldElement.cloneNode(bool deep);
This method has only one parameter deep, a Boolean value. If it is true, clone oldElement and this child node, otherwise only the node itself.
The return value is a cloned node newElement.
The following is the test code, test.htm and test.js files.