blob: 7ce04064f3e2ddbb1db525444e88ebfaf3879801 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
---
title: XPathEvaluator
slug: Web/API/XPathEvaluator
tags:
- API
- DOM
- DOM XPath API
- Document
- Interface
- NeedsTranslation
- Reference
- TopicStub
- XML
- XPath
- XPathEvaluator
translation_of: Web/API/XPathEvaluator
---
<p>{{APIRef("DOM XPath")}}</p>
<p> <code>XPathEvaluator</code> 接口能够对 {{Glossary("XPath")}} 表达式进行编译和求值。</p>
<p>该接口实现自{{domxref("Document")}}的接口。</p>
<h2 id="Methods" name="Methods">方法</h2>
<dl>
<dt>{{DOMxRef("XPathEvaluator.createExpression()")}}</dt>
<dd>创建一个解析过的XPath和解析后的namespaces</dd>
<dt>{{DOMxRef("XPathEvaluator.createNSResolver()")}}</dt>
<dd>任意DOM节点能够通过该方法来解析namespaces,允许通过节点出现在文档中的相对上下文对XPath表达式进行求值。</dd>
<dt>{{DOMxRef("XPathEvaluator.evaluate()")}}</dt>
<dd>对XPath字符串求值,返回可能的确切类型的匹配结果。</dd>
</dl>
<h2 id="例子">例子</h2>
<p>下面的实例展示了如何使用<code>XPathEvaluator</code>接口。</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><div>XPath example</div>
<div>Number of &lt;div&gt;s: <output></output></div>
</pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: 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;
</pre>
<h3 id="结果"> 结果</h3>
<p>{{EmbedLiveSample('Example', 400, 70)}}</p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName("DOM3 XPath", "xpath.html#XPathEvaluator", "XPathEvaluator")}}</td>
<td>{{Spec2("DOM3 XPath")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.XPathEvaluator")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{domxref("document.createExpression()")}}</li>
<li>{{domxref("XPathExpression")}}</li>
</ul>
|