gpt4 book ai didi

javascript - 将 [object HTMLDocument] 转换为字符串

转载 作者:行者123 更新时间:2023-11-27 23:05:47 24 4
gpt4 key购买 nike

我正在创建一个 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 内容转换为字符串格式

演示

jsbin

关于javascript - 将 [object HTMLDocument] 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com