Home >Web Front-end >JS Tutorial >JavaScript code for cloning nodes using cloneNode method_javascript skills

JavaScript code for cloning nodes using cloneNode method_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:49:001759browse

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.

Copy code The code is as follows:




Test of cloneNode Method