Cloning / duplication of html nodes, including their styles

I recently started working on cloning displayed objects.

I noticed that basically there are two ways to clone nodes:

1) I tried to clone the nodes using the cloneNode method, which is pretty good but not supported in older browsers.

var newNode = oldNode.cloneNode(deep);

cloneNode Reference MDN

2) For older browsers, I try to copy outerHTML and set the value to innerHTML. how

newNode.innerHTML = oldNode.outerHTML

Note:

For some older versions of Internet Explorer, innerHTML is a read-only property for table elements.

What is the best of the above methods or is there any other best method. Please help me.

+3
source share
1 answer

jquery $.clone? http://api.jquery.com/clone/

$( "#oldNode" ).clone().appendTo( "#newNode" );

EDIT: ahh , innerHtml

innerHtml

function setTBodyInnerHTML(tbody, html) {
  var temp = tbody.ownerDocument.createElement('div');
  temp.innerHTML = '<table>' + html + '</table>';

  tbody.parentNode.replaceChild(temp.firstChild.firstChild, tbody);
}
0

All Articles