简体   繁体   中英

Jquery clone original HTML not DOM

I need to clone a section of markup that has already been modified in the DOM. I want to get the original HTML source - what is seen in View > Source.

clone() uses what's in the DOM

You can't.

If you need it, store the original markup in a JavaScript var before making any modifications.

Try putting it in a variable before the DOM is manipulated. I use this method for jQuery templating... var template = $('#template').html()

A straightforward approach would be to preserve the original markup in a hidden div . Then, clone that hidden content.

Another option would be to do an AJAX request to retrieve the original document, then load the HTML you want to duplicate from there.

I have written a library for this task some time ago :)

http://www.moyablog.com/innerouter-xhtml/

originalOuterXHTML(element) will accomplish what you want

you would need to copy that HTML into something before the changes are made to the DOM. what the DOM currently has at any given point in time is what you have access to, not past/future versions unless you explicitly saved them to something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM