diff options
Diffstat (limited to 'files/zh-cn/web/api/document/evaluate/index.html')
| -rw-r--r-- | files/zh-cn/web/api/document/evaluate/index.html | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/document/evaluate/index.html b/files/zh-cn/web/api/document/evaluate/index.html new file mode 100644 index 0000000000..49beb37091 --- /dev/null +++ b/files/zh-cn/web/api/document/evaluate/index.html @@ -0,0 +1,168 @@ +--- +title: Document.evaluate() +slug: Web/API/Document/evaluate +tags: + - API + - DOM + - XPath + - 参考 + - 方法 +translation_of: Web/API/Document/evaluate +--- +<div>{{ ApiRef("DOM") }}</div> + +<p>根据传入的 <a href="/zh-CN/docs/XPath">XPath</a> 表达式以及其他参数,返回一个 {{domxref("XPathResult")}} 对象。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox">var xpathResult = document.evaluate( + xpathExpression, + contextNode, + namespaceResolver, + resultType, + result +);</pre> + +<ul> + <li><code>xpathExpression</code> 表示要计算的 Xpath 字符串。</li> + <li><code>contextNode</code> 表示本次查询的<em>上下文节点</em>(参照XPath规范 <a href="http://www.w3.org/TR/xpath">http://www.w3.org/TR/xpath</a>)。通常会使用 <code>document</code>。</li> + <li><code>namespaceResolver</code> 是函数。传入名空间前缀,返回跟此前缀相关的名空间URI(字符串)。通常用来解析Xpath内的前缀,以便对文档进行匹配。HTML文档或者不使用名空间前缀的文档,通常传入 <code>null</code>。</li> + <li><code>resultType</code> 是整数。指定所返回的 <code>XPathResult</code> 的类型,常使用 <a href="#Result_types">named constant properties</a>,如 <code>XPathResult.ANY_TYPE</code>,范围 0 到 9,见下表。</li> + <li><code>result</code> 为 <code>XPathResult</code> 型,用以存储查询结果。通常传入 <code>null</code>,此时将创建新的 <code>XPathResult</code> 对象。</li> +</ul> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">var headings = document.evaluate("/html/body//h2", document, null, XPathResult.ANY_TYPE, null); +/* 在 document 中查找所有的 h2 元素。 + * 结果可能是无序节点迭代器。 */ +var thisHeading = headings.iterateNext(); +var alertText = "Level 2 headings in this document are:\n"; +while (thisHeading) { + alertText += thisHeading.textContent + "\n"; + thisHeading = headings.iterateNext(); +} +alert(alertText); // 显示所有 h2 节点的文本 +</pre> + +<p>注意,在上述例子中,最好写更冗长的XPath,而不是常用的简写,比如 <code>//h2</code>。 通常,像上述例子所示,更具体的XPath选择器会得到显著的性能提升,特别是在非常大的文档中。这是因为查询计算不会将时间浪费在查看不需要的节点上。使用 // 通常很慢,这是因为它要从根节点和所有子节点中查找所有可能匹配的节点。</p> + +<p>通过谨慎使用上下文参数能得到进一步的优化。比如,如果你知道你要查找的内容在 <code>body</code> 标签的某处,你可以这样做:</p> + +<pre class="brush: js">document.evaluate(".//h2", document.body, null, XPathResult.ANY_TYPE, null); +</pre> + +<p>注意上面的 <code>document.body</code> 已经替代了document作为上下文,所以 XPath 从 body 元素开始查找。 (在这个例子中,<code>"."</code> 很重要,因为它指示了查找要从document.body这个上下文节点开始。如果遗漏了 <code>"."</code> (剩下 <code>//h2</code>) ,查找会从根节点(<code>html</code>)处开始,这样会很浪费。)</p> + +<p>查阅 <a href="/en-US/docs/Introduction_to_using_XPath_in_JavaScript" title="Introduction to using XPath in JavaScript">Introduction to using XPath in JavaScript</a> 获得更多信息。</p> + +<h2 id="注意">注意</h2> + +<ul> + <li>XPath 表达式能在 HTML 和 XML 文档上计算。</li> + <li>如果要计算别的而不是当前文档,在 FF3 上必须使用<code>someXMLDoc.evaluate()</code> ,虽然在 FF2 上 <code>document.evaluate()</code> 也有效。</li> +</ul> + +<h2 id="结果的类型">结果的类型</h2> + +<p>(Merge with <a href="/Template:XPathResultConstants">Template:XPathResultConstants</a>?</p> + +<p>这些是 <code>evaluate</code> 方法的 <code>resultType</code> 参数支持的值:</p> + +<table class="standard-table"> + <tbody> + <tr> + <td class="header">Result Type</td> + <td class="header">Value</td> + <td class="header">Description</td> + </tr> + <tr> + <td><code>ANY_TYPE</code></td> + <td>0</td> + <td>Whatever type naturally results from the given expression.</td> + </tr> + <tr> + <td><code>NUMBER_TYPE</code></td> + <td>1</td> + <td>A result set containing a single number. Useful, for example, in an XPath expression using the <code>count()</code> function.</td> + </tr> + <tr> + <td><code>STRING_TYPE</code></td> + <td>2</td> + <td>A result set containing a single string.</td> + </tr> + <tr> + <td><code>BOOLEAN_TYPE</code></td> + <td>3</td> + <td>A result set containing a single boolean value. Useful, for example, an an XPath expression using the <code>not()</code> function.</td> + </tr> + <tr> + <td><code>UNORDERED_NODE_ITERATOR_TYPE</code></td> + <td>4</td> + <td>A result set containing all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.</td> + </tr> + <tr> + <td><code>ORDERED_NODE_ITERATOR_TYPE</code></td> + <td>5</td> + <td>A result set containing all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.</td> + </tr> + <tr> + <td><code>UNORDERED_NODE_SNAPSHOT_TYPE</code></td> + <td>6</td> + <td>A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.</td> + </tr> + <tr> + <td><code>ORDERED_NODE_SNAPSHOT_TYPE</code></td> + <td>7</td> + <td>A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.</td> + </tr> + <tr> + <td><code>ANY_UNORDERED_NODE_TYPE</code></td> + <td>8</td> + <td>A result set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.</td> + </tr> + <tr> + <td><code>FIRST_ORDERED_NODE_TYPE</code></td> + <td>9</td> + <td>A result set containing the first node in the document that matches the expression.</td> + </tr> + </tbody> +</table> + +<p>Results of <code>NODE_ITERATOR</code> types contain references to nodes in the document. Modifying a node will invalidate the iterator. After modifying a node, attempting to iterate through the results will result in an error.</p> + +<p>Results of <code>NODE_SNAPSHOT</code> types are snapshots, which are essentially lists of matched nodes. You can make changes to the document by altering snapshot nodes. Modifying the document doesn't invalidate the snapshot; however, if the document is changed, the snapshot may not correspond to the current state of the document, since nodes may have moved, been changed, added, or removed.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("DOM3 XPath", "xpath.html#XPathEvaluator-evaluate", "Document.evaluate")}}</td> + <td>{{Spec2("DOM3 XPath")}}</td> + <td>Initial specification</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Document.evaluate")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("Document.createExpression()")}}</li> + <li>{{domxref("XPathResult")}}</li> + <li><a href="/zh-CN/docs/Code_snippets/XPath">XPath Code Snippets</a></li> + <li><a href="http://codepen.io/johan/full/ckFgn">Check for browser support</a></li> +</ul> |
