如何使用jQuery获取整个页面的HTML?

时间:2021-06-15 21:22:45

I used $(document).html(), but that threw an error... is there a way to get everything?

我使用了$(document).html(),但这引发了一个错误......有没有办法获得一切?

4 个解决方案

#1


57  

You could try:

你可以尝试:

$("html").html();

If you want to also capture the hmtl tags you could concatenate them to the html like this:

如果你想捕获hmtl标签,你可以将它们连接到html,如下所示:

function getPageHTML() {
  return "<html>" + $("html").html() + "</html>";
}

#2


63  

Don't forget the <html> tag can have attributes too. If you want the whole document this should work.

不要忘记标签也可以具有属性。如果你想要整个文件这应该工作。

 $('html')[0].outerHTML

It's also trivial without jQuery.

没有jQuery,它也是微不足道的。

document.documentElement.outerHTML

If you also want to include the doctype, it's a little more involved.

如果您还想要包含doctype,那么它会更复杂一些。

var getDocTypeAsString = function () { 
    var node = document.doctype;
    return node ? "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>\n' : '';
};

getDocTypeAsString() + document.documentElement.outerHTML   

#3


4  

Use:

使用:

document.body.innerHTML

#4


2  

$("html").html() would get everything but the outer most html tags.

$(“html”)。html()除了最外层的html标签外,还能获得所有内容。

#1


57  

You could try:

你可以尝试:

$("html").html();

If you want to also capture the hmtl tags you could concatenate them to the html like this:

如果你想捕获hmtl标签,你可以将它们连接到html,如下所示:

function getPageHTML() {
  return "<html>" + $("html").html() + "</html>";
}

#2


63  

Don't forget the <html> tag can have attributes too. If you want the whole document this should work.

不要忘记标签也可以具有属性。如果你想要整个文件这应该工作。

 $('html')[0].outerHTML

It's also trivial without jQuery.

没有jQuery,它也是微不足道的。

document.documentElement.outerHTML

If you also want to include the doctype, it's a little more involved.

如果您还想要包含doctype,那么它会更复杂一些。

var getDocTypeAsString = function () { 
    var node = document.doctype;
    return node ? "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>\n' : '';
};

getDocTypeAsString() + document.documentElement.outerHTML   

#3


4  

Use:

使用:

document.body.innerHTML

#4


2  

$("html").html() would get everything but the outer most html tags.

$(“html”)。html()除了最外层的html标签外,还能获得所有内容。