您正位於 MDN Web Docs 的可編輯版本

以 MDN Web Docs 使用者身分檢視: https://developer.mozilla.org/zh-TW/docs/Web/API/XPathEvaluator

我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

The XPathEvaluator interface allows to compile and evaluate XPath expressions.

It is implemented by the Document interface.

Methods

XPathEvaluator.createExpression()
Creates a parsed XPath expression with resolved namespaces.
XPathEvaluator.createNSResolver()
Adapts any DOM node to resolve namespaces allowing the XPath expression to be evaluated relative to the context of the node where it appeared within the document.
XPathEvaluator.evaluate()
Evaluates an XPath expression string and returns a result of the specified type if possible.

Example

The following example shows the use of the XPathEvaluator interface.

HTML

<div>XPath example</div>
<div>Number of &lt;div&gt;s: <output></output></div>

JavaScript

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;

Result

Specifications

Specification Status Comment
Document Object Model (DOM) Level 3 XPath Specification
The definition of 'XPathEvaluator' in that specification.
Recommendation Initial definition

Browser compatibility

No compatibility data found. Please contribute data for "api.XPathEvaluator" (depth: 1) to the MDN compatibility data repository.

See also

文件標籤與貢獻者

此頁面的貢獻者: mdnwebdocs-bot, Sebastianz
最近更新: mdnwebdocs-bot,