XPathEvaluator
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
方法
XPathEvaluator.createExpression()
-
创建一个解析过的 XPath 和解析后的 namespaces
XPathEvaluator.createNSResolver()
-
任意 DOM 节点能够通过该方法来解析 namespaces,允许通过节点出现在文档中的相对上下文对 XPath 表达式进行求值。
XPathEvaluator.evaluate()
-
对 XPath 字符串求值,返回可能的确切类型的匹配结果。
示例
下面的实例展示了如何使用XPathEvaluator
接口。
HTML
html
<div>XPath example</div>
<div>Number of <div>s: <output></output></div>
JavaScript
js
var xpath = "//div";
var evaluator = new XPathEvaluator();
var expression = evaluator.createExpression("//div");
var result = expression.evaluate(
document,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
);
document.querySelector("output").textContent = result.snapshotLength;
结果
规范
Specification |
---|
DOM Standard # interface-xpathevaluator |
浏览器兼容性
BCD tables only load in the browser