简体   繁体   English

如何从javascript中的[object HTMLDocument]获取内容

[英]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.

相关问题 如何显示来自 Rails AJAX JSON 响应的文本 - Javascript 正在返回 [object HTMLDocument] - How do I display the text from a Rails AJAX JSON response - Javascript is returning [object HTMLDocument] javascript操作后获取HtmlDocument - Get HtmlDocument after javascript manipulations 如何从Android或Java中的[object HTMLDocument]转换字符串 - How to convert string from [object HTMLDocument] in android or java 如何从JavaScript函数导出到内容的文档对象? - How to get document object from JavaScript function exported to content? 如何在mshtml.HTMLDocument(.NET)中禁用Javascript - How to disable Javascript in mshtml.HTMLDocument (.NET) 如何从网站获取JavaScript动态内容 - How to get javascript dynamic content from website 如何从 Javascript 中 Firefox 上的剪贴板获取内容 - How to get the content from clipboard on Firefox in Javascript 使用JavaScript获取对象XMLHttpRequest的内容 - Get content of object XMLHttpRequest with javascript 无法在HTMLDocument中设置对象变量以从网站抓取数据 - Can't set object variable in HTMLDocument for scraping data from website javascript - 如何从 FormData object [object Object] 中获取嵌套的 object - javascript - How to get nested object from a FormData object [object Object]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM