This interface is a compiled XPath expression that can be evaluated on a document or specific node to return information from its DOM 树。 This is useful when an expression will be reused in an application, because it is just compiled once and all namespace prefixes which occur within the expression are preresolved.

Objects of this type are created by calling XPathEvaluator.createExpression() .

方法

XPathExpression.evaluate()

Evaluates the XPath expression on the given node or document.

范例

The following example shows the use of the XPathExpression 接口。

HTML

<div>XPath example</div>
<div>Number of <div>s: <output></output></div>
					

JavaScript

var xpath = "//div";
var evaluator = new XPathEvaluator();
var expression = evaluator.createExpression(xpath);
var result = expression.evaluate(document, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
document.querySelector("output").textContent = result.snapshotLength;
					

结果

规范

规范 状态 注释
DOM (文档对象模型) 3 级 XPath 规范
The definition of 'XPathExpression' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
XPathExpression Chrome Yes Edge ≤18 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
evaluate Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: