Javascript DOM tree duplicate for manipulation

后端 未结 3 657
旧时难觅i
旧时难觅i 2021-01-05 23:42

Since the DOM tree of a page is active and always reflected in the browser, what is the best way to modify this DOM tree for some purpose without affecting the actual render

相关标签:
3条回答
  • 2021-01-05 23:55

    Maybe consider one the many great JavaScript librarys out there, e.g. jQuery. These allow you to easily copy parts of or even the whole DOM of an document and have that stored appart from the DOM.

    If you need to roll your own solution, a good point to start is Resig's post on document fragments: http://ejohn.org/blog/dom-documentfragments/.

    Good luck.

    0 讨论(0)
  • 2021-01-06 00:00

    You can use document.cloneNode(true), or the same method on another node. cloneNode clones any node, and the true means it should be recursive (deep). Obviously, this could have a significant performance cost on a large page.

    0 讨论(0)
  • 2021-01-06 00:05

    If you are willing to use jQuery:

    var clone = $("selectorForSomeElement(s)").clone();
    

    clone now is a copy of the element structure.

    You can then work off of clone to do whatever experimenting you like.

    0 讨论(0)
提交回复
热议问题