简体   繁体   中英

Retrieve the full page HTML - jQuery

I am trying to retrieve the full HTML of a page, so far I am using:

$('*').html()

Is there a better way (also this excludes the <html> tags at the top)?

I tried $('html').html() but this of course excludes the html element.

$('html').parent().html() didn't work either because the root has no parents.

$(document).html() also fails

You could get the contents and then capture any attributes from the html element that you require, at leas then you can rebuild it if needed

Edit based on comment: Just found this cross browser OuterHTML plugin http://yelotofu.com/2008/08/jquery-outerhtml/

Not sure what you're using this for, but would using $.get to fetch the location.href be a reasonable workaround (as a bonus you'd get the DOCTYPE and other header-ish stuff as well).

$.get(location.href, function(html) {
    alert(html);
});

Ok maybe that's kinda silly...

function GetPageHtml()
{
   return $('html')[0] + $('html').html() + '</html>';
}

这可以解决问题:

html = $('<dummy>').append($('html').clone()).remove().html();

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