[英]How to get content from [object HTMLDocument] in javascript
I'm trying to fetch an HTML data (which I parsed from string because the javascript files linked to it doesn't work) from a url, then load the response into document.我试图从一个 url 获取一个 HTML 数据(我从字符串中解析它,因为链接到它的 javascript 文件不起作用),然后将响应加载到文档中。 However, when I log the response in the console, I get the html content but it displays [object HTMLDocument] when I load the document.但是,当我在控制台中记录响应时,我得到了 html 内容,但在我加载文档时它会显示 [object HTMLDocument]。
Here is my code -这是我的代码 -
fetch(url)
.then(res => res.text())
.then(data => {
let parsedRes = (new window.DOMParser()).parseFromString(data, "text/html")
processData(parsedRes, url)
});
function processData(response, urlPath){
console.log(response)
document.querySelector("html").innerHTML = response;
window.history.pushState({}, "", urlPath);
};
How can this be achieved?如何做到这一点?
response
is a document object, innerHTML expects a string. response
是一个文档对象,innerHTML 需要一个字符串。 You could use the inner html of the response
document...您可以使用response
文档的内部 html ......
fetch(`data:text/html;,<html><head>\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.