有没有一种方法在JS中获取整个HTML作为字符串,包括html标签内的内容?
document.documentElement.??
有没有一种方法在JS中获取整个HTML作为字符串,包括html标签内的内容?
document.documentElement.??
微软在一段时间前添加了outerHTML
和innerHTML
属性。
根据MDN,outerHTML
在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);
你可以做到
new XMLSerializer().serializeToString(document)
在IE 9以上的浏览器中
我尝试了各种答案以查看返回的内容。我正在使用最新版本的Chrome。
建议document.documentElement.innerHTML;
返回<head>...</body>
Gaby的建议document.getElementsByTagName('html')[0].innerHTML;
返回相同内容。
建议document.documentElement.outerHTML;
返回除了“doctype”之外的所有内容<html><head> ... </body></html>
。
你可以使用document.doctype;
来检索doctype对象。 这会返回一个对象,而不是字符串,因此,如果需要提取所有doctype(包括HTML5)的详细信息作为字符串,则在此处描述:使用Javascript获取HTML的DocType作为字符串
我只想要HTML5,因此以下内容对我来说已足够创建整个文档:
alert('<!DOCTYPE HTML>' + '\n' + document.documentElement.outerHTML);
document.documentElement.outerHTML
应该为您返回结果。outerHTML
在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.getElementsByTagName('html')[0].innerHTML
你将无法获取文档类型或html标签,但可以获得其他所有内容...
document.documentElement.outerHTML
outerHTML
在DOM解析和序列化规范中。 - XP1可能只在IE浏览器中出现:
> webBrowser1.DocumentText
从 1.0 版本开始使用的 FF:
//serialize current DOM-Tree incl. changes/edits to ss-variable
var ns = new XMLSerializer();
var ss= ns.serializeToString(document);
alert(ss.substr(0,300));
可能在火狐浏览器中工作。(显示源文本最开始的前300个字符,主要是doctype-defs)
但请注意,火狐浏览器的正常“另存为”对话框可能不会保存当前页面状态,而是原始加载的X/h/tml源文本!(将ss POST到某个临时文件并重定向到该文件可以提供可保存的源文本和之前进行过的更改/编辑。)
尽管火狐浏览器令人惊喜地通过“返回”实现了良好的恢复,并且对于类似输入字段、文本区域等的内容可以在“保存(为)...”中包含状态/值,但不能在contenteditable/designMode中的元素中使用...
如果不是xhtml- resp. xml文件(mime类型,不仅仅是文件扩展名!), 可以使用document.open/write/close将合适的内容设置为源层,这将在用户从火狐浏览器的文件/保存菜单中保存时保存。
参见:http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite 或者 https://developer.mozilla.org/en-US/docs/Web/API/document.write
对于X(ht)ML问题中立,尝试将“view-source:http://...”作为一个(由脚本制作的?)iframe的src属性值,以在火狐浏览器中访问iframe文档:
<iframe-elementnode>.contentDocument
,查看google“mdn contentDocument”以获取适当的成员,例如'textContent'。
document.documentElement.innerHTML
<html ...>
标签。 - Dan Dascalescu<html>...</html>
之外的内容,尤其是 <!DOCTYPE ...>
声明,您可以遍历 document.childNodes,并将每个节点转换为字符串:const html = [...document.childNodes]
.map(node => nodeToString(node))
.join('\n') // could use '' instead, but whitespace should not matter.
function nodeToString(node) {
switch (node.nodeType) {
case node.ELEMENT_NODE:
return node.outerHTML
case node.TEXT_NODE:
// Text nodes should probably never be encountered, but handling them anyway.
return node.textContent
case node.COMMENT_NODE:
return `<!--${node.textContent}-->`
case node.DOCUMENT_TYPE_NODE:
return doctypeToString(node)
default:
throw new TypeError(`Unexpected node type: ${node.nodeType}`)
}
}
doctypeToString
的函数;其实现可以如下(下面的代码已经在npm上发布为doctype-to-string):function doctypeToString(doctype) {
if (doctype === null) {
return ''
}
// Checking with instanceof DocumentType might be neater, but how to get a
// reference to DocumentType without assuming it to be available globally?
// To play nice with custom DOM implementations, we resort to duck-typing.
if (!doctype
|| doctype.nodeType !== doctype.DOCUMENT_TYPE_NODE
|| typeof doctype.name !== 'string'
|| typeof doctype.publicId !== 'string'
|| typeof doctype.systemId !== 'string'
) {
throw new TypeError('Expected a DocumentType')
}
const doctypeString = `<!DOCTYPE ${doctype.name}`
+ (doctype.publicId ? ` PUBLIC "${doctype.publicId}"` : '')
+ (doctype.systemId
? (doctype.publicId ? `` : ` SYSTEM`) + ` "${doctype.systemId}"`
: ``)
+ `>`
return doctypeString
}
我总是使用
document.getElementsByTagName('html')[0].innerHTML
可能不是正确的方式,但当我看到它时我能理解它。
<html...>
标签。 - Dan Dascalescu
document.body.parentElement.innerHTML
翻译为:文档.正文.父元素.内部HTML。 - Radvylf Programs&&
替换为&&
,这会破坏所有内联<script>
标签!你应该使用document.documentElement.outerHTML
,但请注意它不会获取<!DOCTYPE html>
,所以你需要自己添加。 - joe