XHR's 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
method?
它本身不支持fetch
因为该 API 是纯粹的网络层 API,不依赖于 Web 浏览器(参见讨论 https://github.com/whatwg/fetch/issues/16),但实现起来并不难:
fetch('/').then(res => res.text())
.then(text => new DOMParser().parseFromString(text, 'text/html'))
.then(document => {
const h2headings = document.querySelectorAll('h2');
// ...
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)