How to clone (and restore) a DOM subtree

前端 未结 4 2003
别那么骄傲
别那么骄傲 2021-01-04 04:30

I would like to modify a DOM subtree and restore it after a while. How can I save a sub-tree copy aside (to play with the actual subtree)? How can I restore the saved copy a

4条回答
  •  Happy的楠姐
    2021-01-04 05:11

    If I'm reading this right, then I think all you'd need to do is:

    var DomTreeCopy = $('parentElementSelector').clone(true,true);
    

    And then to re-attach the DomTreeCopy (in place of the original):

    $('parentElementSelector').replaceWith(DomTreeCopy);
    

    Or to add it to the DOM in addition to the original:

    $(DomTreeCopy).insertAfter($('parentElementSelector'));
    

    References:

    • clone().
    • insertAfter().
    • replaceWith().

提交回复
热议问题