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

document.documentElement.??

当前回答

使用document.documentElement。

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

其他回答

你还可以:

document.getElementsByTagName('html')[0].innerHTML

你不会得到Doctype或html标签,但其他的一切…

使用document.documentElement。

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

你可以这样做

new XMLSerializer().serializeToString(document)

在比ie9更新的浏览器中

看到https://caniuse.com/壮举= xml序列化

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

在VBA中是这样的

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

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

MS在一段时间前添加了outerHTML和innerHTML属性。

MDN表示,目前支持的浏览器包括Firefox 11、Chrome 0.2、Internet Explorer 4.0、Opera 7、Safari 1.3、Android、Firefox Mobile 11、IE Mobile、Opera Mobile、Safari Mobile等。outerHTML在DOM解析和序列化规范中。

查看quirksmode浏览器兼容性,了解适合您的浏览器。都支持innerHTML。

var markup = document.documentElement.innerHTML;
alert(markup);