我需要構建一個腳本(完全 JS,不允許使用 jQuery),它將在頁面上執行一些操作,讓我們將其稱為活動頁面,基于存盤在不同頁面(同一域)上的資料,將其稱為被動頁面.
這些資料存盤在被動頁面的特定 HTML 元素中。
我對 Ajax 完全陌生,所以在開始之前我試圖在互聯網上進行研究,但沒有發現任何有用的東西(或者,也許,我無法理解)。
如何檢索我需要的特定資料?
我被這個小代碼困住了,但是它只回傳了頁面“骨架”:
javascript:
var myAttempt = new XMLHttpRequest();
myAttempt.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myAttempt = this.responseText;
}
}
myAttempt.open("GET", "https://www.website.com/passivepage.html", true);
myAttempt.send();
我應該在哪里告訴腳本尋找被動頁面的特定元素?
謝謝
uj5u.com熱心網友回復:
通過您的 ajax 請求,您會收到一個字串作為回應,要在其中查找特定元素,您可以創建一個虛擬 DOM 元素并將該字串添加到其中,然后在虛擬元素中找到您要查找的每個元素。像這樣:
var myAttempt = new XMLHttpRequest();
myAttempt.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var div = document.createElement('div');
div.style.display = 'none';
div.innerHTML = this.responseText;
document.body.append(div);
const theElement = div.querySelector('selector');
alert(theElement.getAttribute('x'));
}
}
myAttempt.open("GET", "/url", true);
myAttempt.send();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/377897.html
