gpt4 book ai didi

javascript - 使用 document.evaluate() 的 xpathResult 作为 contextNode 不起作用

转载 作者:行者123 更新时间:2023-11-29 10:59:40 27 4
gpt4 key购买 nike

在使用 document.evaluate 时,我再次尝试将 xpathResult 用作与所附示例类似的上下文节点,但没有成功,我哪里错了?

const getAllLiButton = document.querySelector("#get-all-li");
const getTitleAndTextButton = document.querySelector("#get-title-text");

getAllLiButton.addEventListener("click", getAllLi);
getTitleAndTextButton.addEventListener("click", getTitleAndText);

function getAllLi() {
var records = [];

let topLevelResults = document.evaluate("/html/body/ul/li", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var levelResult = topLevelResults.iterateNext();
while (levelResult) {
records.push(levelResult);

levelResult = topLevelResults.iterateNext();
}

console.log(records);
}

function getTitleAndText() {
let topLevelResults = document.evaluate("/html/body/ul/li", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var levelResult = topLevelResults.iterateNext();

let titleText = document.evaluate("/h3", levelResult, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
console.log(titleText);

let textBox = document.evaluate("/span", levelResult, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
console.log(textBox);
}
<button id="get-all-li">Get all li (works)</button>
<button id="get-title-text">Get single Title & Text (not works)</button>

<ul>
<li><h3>Title #1</h3><span>some text</span></li>
<li><h3>Title #2</h3><span>some text</span></li>
<li><h3>Title #3</h3><span>some text</span></li>
<li><h3>Title #4</h3><span>some text</span></li>
<li><h3>Title #5</h3><span>some text</span></li>
</ul>

预先感谢您的帮助!

最佳答案

documentation 中所述:

document.evaluate(".//h3", levelResult, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null)

.//h3 xpathExpression 很重要,它指示查询应从上下文节点(levelResult)开始。

const getAllLiButton = document.querySelector("#get-all-li");
const getTitleAndTextButton = document.querySelector("#get-title-text");

getAllLiButton.addEventListener("click", getAllLi);
getTitleAndTextButton.addEventListener("click", getTitleAndText);

function getAllLi() {
var records = [];

let topLevelResults = document.evaluate("/html/body/ul/li", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var levelResult = topLevelResults.iterateNext();
while (levelResult) {
records.push(levelResult);

levelResult = topLevelResults.iterateNext();
}

console.log(records);
}

function getTitleAndText() {
let topLevelResults = document.evaluate("/html/body/ul/li", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var levelResult = topLevelResults.iterateNext();

let titleText = document.evaluate(".//h3", levelResult, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
titleText = titleText.iterateNext();
console.log(titleText.textContent);

let textBox = document.evaluate(".//span", levelResult, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
textBox = textBox.iterateNext();
console.log(textBox.textContent);
}
<button id="get-all-li">Get all li (works)</button>
<button id="get-title-text">Get single Title & Text (not works)</button>

<ul>
<li><h3>Title #1</h3><span>some text</span></li>
<li><h3>Title #2</h3><span>some text</span></li>
<li><h3>Title #3</h3><span>some text</span></li>
<li><h3>Title #4</h3><span>some text</span></li>
<li><h3>Title #5</h3><span>some text</span></li>
</ul>

关于javascript - 使用 document.evaluate() 的 xpathResult 作为 contextNode 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49678056/

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