我正在创建一个 HTML 文档对象:
let newHTMLDocument = document.implementation.createHTMLDocument();
let html = `<!DOCTYPE html>
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>`;
newHTMLDocument.open();
newHTMLDocument.write( html );
newHTMLDocument.close();
console.log( String(newHTMLDocument) ); // [object HTMLDocument]
我如何才能将 newHTMLDocument 转换为包含所有 HTML 代码(包括 doctype 和 html 标记)的字符串,而不是“[object HTMLDocument]”?
最佳答案
试试这个 console.log(new XMLSerializer().serializeToString(newHTMLDocument))
它将从文档中抓取 html 内容转换为字符串格式
演示
关于javascript - 将 [object HTMLDocument] 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472323/