컴퓨터/JavaScript_typescript
How to make an exact copy of an element?
수제녹차
2020. 3. 25. 09:58
728x90
반응형
* deep copy
// Node.cloneNode()
var elem = document.querySelector("#node1");
// optional boolean argument, deep
// true -> the child elements (including text nodes) are copied, too
var copy = elem.cloneNode(true);
// modify the id
copy.id = 'node2';
//insert into the DOM before the original
elem.parentNode.insertBefore(copy, elem);
* shallow copy
// cloneNode(false) -> no child nodes are copied
// but with all of the ids, classes, and other attributes still on it
// make a shallow copy
var copy = elem.cloneNode(); // same as "false" argument
elem.parentNode.insertBefore(copy, elem);
source : [Go Make Things]
반응형