在JS中是否有一种方法可以在HTML标签中获取整个HTML,作为字符串?

document.documentElement.??

当前回答

document.documentElement.innerHTML

其他回答

如果你想获取DOCTYPE之外的所有内容,这将有效:

document.getElementsByTagName('html')[0].outerHTML;

如果你想要doctype也可以这样:

new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML;

使用document.documentElement。

这里回答了同样的问题: https://stackoverflow.com/a/7289396/2164160

您必须遍历文档childNodes并获得outerHTML内容。

在VBA中是这样的

For Each e In document.ChildNodes
    Put ff, , e.outerHTML & vbCrLf
Next e

使用这个,允许你获得网页的所有元素,包括< !DOCTYPE >节点(如果它存在的话)

document.documentElement.innerHTML
document.documentElement.outerHTML