使用 JavaScript/JQuery 获取整个文档的 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14621243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:08:17  来源:igfitidea点击:

Get the whole document's html with JavaScript/JQuery

javascriptjquery

提问by dtrunk

I know it's already discussed here, but there were no solution to get the whole document (including doctype).

我知道这里已经讨论过了,但是没有获得整个文档(包括 doctype)的解决方案。

$(document).html();returns null...

$(document).html();返回null...

回答by Jan Han?i?

This will get you all the HTML:

这将为您提供所有 HTML:

document.documentElement.outerHTML

Unfortunately it does not return the doctype. But you can use document.doctypeto get it and glue the two together.

不幸的是,它不返回文档类型。但是你可以用document.doctype它来把两者粘在一起。

回答by Erik Aigner

You can do

你可以做

new XMLSerializer().serializeToString(document);

for all browsers newer than IE 9

适用于所有比 IE 9 更新的浏览器

回答by Mortalus

try this.

试试这个。

$("html").html()

document is a variable it dose not represent the html tag.

document 是一个变量,它不代表 html 标签。

EDIT

编辑

To get the doctype one could use

要获得可以使用的文档类型

document.doctype

回答by Downgoat

This is a function which has support in IE6+, it does't use outerHTMLfor even more support, it adds the doctype and uses a few tricks to get the htmltag and its attributes. In order to receive a string with the doctype, and doesn't use outerHTMLso it supports every browser. It uses a few tricks to get the htmltag. Add this code:

这是一个IE6+支持的功能,它并没有使用outerHTML更多的支持,它添加了doctype并使用了一些技巧来获取html标签及其属性。为了接收带有 doctype 的字符串,并且不使用outerHTML所以它支持每个浏览器。它使用一些技巧来获取html标签。添加此代码:

document.fullHTML = function () {
    var r = document.documentElement.innerHTML, t = document.documentElement.attributes, i = 0, l = '',
        d = '<!DOCTYPE ' + document.doctype.name + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '') + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '') + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '') + '>';
    for (; i < t.length; i += 1) l += ' ' + t[i].name + '="' + t[i].value + '"';
    return d+'\n<html' + l + '>' + r + '</html>';
}

Now, you can run this function:

现在,您可以运行此函数:

console.log(document.fullHTML());

This will return the HTML and doctype.

这将返回 HTML 和 doctype。

I ran this on example.com, here are the results

我在example.com上运行了这个,这是结果

回答by Ph0en1x

document.documentElement.innerHTML 

will return you all document markup as string

会将所有文档标记作为字符串返回

to get the whole doctype read this

让整个文档类型阅读这个

回答by Milind Anantwar

I'm not sure about getting the complete doc.but what you can do is,you can get the content of html tag seprately and doctype seprately.

我不确定是否可以获得完整的文档。但是你可以做的是,你可以单独获取 html 标签的内容和单独的 doctype。

$('html').html() for contentand document.doctypefor getting the doctype

$('html').html() for contentdocument.doctype用于获取文档类型

回答by Denys Séguret

I don't think there is a direct access to the whole document (including the doctype), but this works :

我不认为可以直接访问整个文档(包括文档类型),但这有效:

$.get(document.location, function(html) {
    // use html (which is the complete source code, including the doctype)
});

回答by Ashok Raj

I have done it on browser's console

我已经在浏览器的控制台上完成了

document.documentElement;

文档.documentElement;