diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/nodelist/foreach | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/nodelist/foreach')
-rw-r--r-- | files/zh-cn/web/api/nodelist/foreach/index.html | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/nodelist/foreach/index.html b/files/zh-cn/web/api/nodelist/foreach/index.html new file mode 100644 index 0000000000..29f2c575a7 --- /dev/null +++ b/files/zh-cn/web/api/nodelist/foreach/index.html @@ -0,0 +1,120 @@ +--- +title: NodeList.prototype.forEach() +slug: Web/API/NodeList/forEach +translation_of: Web/API/NodeList/forEach +--- +<p>{{APIRef("DOM")}}</p> + +<p>{{domxref("NodeList")}}接口的 <strong><code>forEach()</code></strong> 方法按插入顺序为列表中的每个值对调用一次参数中给定的回调。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>someNodeList</var>.forEach(<var>callback</var>[, <var>thisArg</var>]); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><var>callback</var></code></dt> + <dd> + <p>为 <code><var>someNodeList</var></code>的每一个元素执行函数。它接受以下三个参数:</p> + + <dl> + <dt><code><var>currentValue</var></code></dt> + <dd><code><var>someNodeList</var></code>中的当前元素。</dd> + <dt><code><var>currentIndex</var></code> {{Optional_inline}}</dt> + <dd><code><var>someNodeList</var></code>中的<code><var>currentValue</var></code>所对应的索引值。</dd> + <dt><code><var>listObj</var></code> {{Optional_inline}}</dt> + <dd><code><var>someNodeList</var></code> 在 <code>forEach()</code> 方法中所属的NodeList对象。</dd> + </dl> + </dd> + <dt><code><var>thisArg</var></code> {{Optional_inline}}</dt> + <dd>传递 <code><var>callback</var></code> 的值一般用{{jsxref("this")}}值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref('undefined')}}.</p> + +<h2 id="Exceptions">Exceptions</h2> + +<p>None.</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js;highlight:[6]">let node = document.createElement("div"); +let kid1 = document.createElement("p"); +let kid2 = document.createTextNode("hey"); +let kid3 = document.createElement("span"); + +node.appendChild(kid1); +node.appendChild(kid2); +node.appendChild(kid3); + +let list = node.childNodes; + +list.forEach( + function(currentValue, currentIndex, listObj) { + console.log(currentValue + ', ' + currentIndex + ', ' + this); + }, + 'myThisArg' +);</pre> + +<p>上述代码会产生以下结果:</p> + +<pre>[object HTMLParagraphElement], 0, myThisArg +[object Text], 1, myThisArg +[object HTMLSpanElement], 2, myThisArg</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>{{Glossary("Polyfill","polyfill")}} 增加了对所有支持<a href="https://caniuse.com/#search=es5">ES5</a>的浏览器的兼容性:</p> + +<pre class="brush: js">if (window.NodeList && !NodeList.prototype.forEach) { + NodeList.prototype.forEach = function (callback, thisArg) { + thisArg = thisArg || window; + for (var i = 0; i < this.length; i++) { + callback.call(thisArg, this[i], i, this); + } + }; +}</pre> + +<p>或者</p> + +<pre class="brush: js">if (window.NodeList && !NodeList.prototype.forEach) { + NodeList.prototype.forEach = Array.prototype.forEach; +}</pre> + +<p>上面的代码是大部分浏览器实现的 <code>NodeList.prototype.forEach()</code> (例如Chrome).</p> + +<h2 id="Specifications">Specifications</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("WebIDL", "#es-forEach", "forEach")}}</td> + <td>{{Spec2("WebIDL")}}</td> + <td>Defines <code>forEach</code> on <code>iterable</code> declarations</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_Compatibility">Browser Compatibility</h2> + + + +<p>{{Compat("api.NodeList.forEach")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("Node")}}</li> + <li>{{domxref("NodeList")}}</li> +</ul> |