XPathExpression

This interface is a compiled XPath expression that can be evaluated on a document or specific node to return information from its DOM tree. 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;
					

结果

规范

规范 状态 Comment
Document Object Model (DOM) Level 3 XPath Specification
The definition of 'XPathExpression' in that specification.
Recommendation 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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

图例

完整支持
完整支持
兼容性未知
兼容性未知

另请参阅