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

document.documentElement.??

当前回答

你还可以:

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

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

其他回答

你可以这样做

new XMLSerializer().serializeToString(document)

在比ie9更新的浏览器中

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

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);

正确的做法其实是:

webBrowser1。DocumentText

我相信document.documentElement.outerHTML应该为您返回该值。

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解析和序列化规范中。

outerHTML属性上的MSDN页面指出IE 5+支持它。Colin的回答链接到W3C quirksmode页面,该页面很好地比较了跨浏览器兼容性(也有其他DOM特性)。

使用document.documentElement。

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