gpt4 book ai didi

xmlhttprequest - fetch() 可以做 responseType=document 吗?

转载 作者:行者123 更新时间:2023-12-05 00:50:47 25 4
gpt4 key购买 nike

XHR的responseType='document'非常棒,因为它为您提供了一个 DOM 文档,您可以在该文档上使用 querySelector 等:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/', true);
xhr.responseType = 'document';
xhr.onload = function(e) {
var document = e.target.response;
var h2headings = document.querySelectorAll('h2');
// ...
};

这可以通过 fetch 实现吗?方法?

最佳答案

fetch 本身不支持它因为 API 是一个纯粹的网络层 API,不依赖于 Web 浏览器( see discussion ),但实现起来并不难:

fetch('/').then(res => res.text())
.then(text => new DOMParser().parseFromString(text, 'text/html'))
.then(document => {
const h2headings = document.querySelectorAll('h2');
// ...
});

关于xmlhttprequest - fetch() 可以做 responseType=document 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512102/

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