2017-08-04 25 views
4

XHR的responseType='document',因爲它給了你回來,你可以使用querySelector DOM文檔是真棒,等上:可以獲取()做responseType = document嗎?

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方法?

回答

4

它本身不支持在fetch的API是在網絡瀏覽器(see discussion)是不依賴純粹的網絡層API,但它不是太難拉斷:

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