将[object HTMLDocument]转换为String

时间:2018-03-25 03:44:38

标签: javascript html dom

我正在创建一个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]
&#13;
&#13;
&#13;

而不是&#34; [对象HTMLDocument],&#34;如何将newHTMLDocument转换为包含所有HTML代码的字符串,包括doctype和html标签?

3 个答案:

答案 0 :(得分:2)

试试这个let operation1 = BlockOperation { //Gets Data from server & waits until task is complete before ending } let operation2 = BlockOperation { //Parse JSON } let updateOperation = BlockOperation { self.tableView.reloadData() //Throws a "UITableView.reloadData() must be used from main thread only" } operation2.addDependency(operation1) updateOperation.addDependency(operation2) let queue = OperationQueue() queue.addOperation(operation1) queue.addOperation(operation2) queue.addOperation(completionOperation) 它会将文档中的html内容抓取为字符串格式

演示

jsbin

答案 1 :(得分:0)

您可以使用console.log(newHTMLDocument.documentElement);

答案 2 :(得分:0)

你可以使用element.do这个东西的innerhtml属性。

ele = newHTMLDocument.getElementByTagName('html') console.log(ele[0].innerHTML);