在使用document.evaluate时,我尝试再次将xpathResult用作contextNode,类似于所附示例,但未成功,我在哪里错了?
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/