JS: difference between document.evaluate(".//html"

JS: difference between document.evaluate(".//html"... and document.evaluate("//html"...?

本文关键字:quot html evaluate difference between document JS      更新时间:2023-09-26

点(.(在document.evaluate中重要吗?我测试过,它似乎什么也没做

Mozilla 教程在示例中使用它 https://developer.mozilla.org/en-US/docs/Web/API/document.evaluate

点是指您所在的元素树中当前选定的节点。

如果您查看函数定义,则如下所示

var xpathResult = document.evaluate(
 xpathExpression, 
 *contextNode*, 
 namespaceResolver, 
 resultType, 
 result
);

通常将文档作为上下文对象传入。如果您还注意到您链接的网站上的以下行

在此示例中,"." 对于指示查询应从上下文节点开始非常重要

当然,所有这些都源于XPath。

如果您不想使用 xpath: https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector,也许您想查看查询选择器