aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/nodelist/keys/index.html
blob: ac08a2ba2dd8126718d57a34a8060db9bded2c99 (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
---
title: NodeList.keys()
slug: Web/API/NodeList/keys
tags:
  - DOM
  - Iterator
  - Method
  - NodeList
  - Reference
  - Web
translation_of: Web/API/NodeList/keys
---
<p>{{APIRef("DOM")}}</p>

<p><code><strong>NodeList.keys()</strong></code> 方法返回 {{jsxref("Iteration_protocols",'iterator')}} ,此方法允许遍历这个对象中包含的所有的键,即使这个键是 <code>unsigned integer(无符号整数)</code>.</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">nodeList.keys();</pre>

<h3 id="返回值">返回值</h3>

<p>返回 {{jsxref("Iteration_protocols","iterator")}}.</p>

<h2 id="例子">例子</h2>

<pre class="brush: js;highlight:[13] notranslate">var node = document.createElement("div");
var kid1 = document.createElement("p");
var kid2 = document.createTextNode("hey");
var kid3 = document.createElement("span");

node.appendChild(kid1);
node.appendChild(kid2);
node.appendChild(kid3);

var list = node.childNodes;

// Using for..of
for(var key of list.keys()) {
   console.log(key);
}
</pre>

<p>结果是:</p>

<pre class="notranslate">0
1
2
</pre>

<h2 id="浏览器兼容">浏览器兼容</h2>

<p>{{Compat("api.NodeList.keys")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{domxref("Node")}}</li>
 <li>{{domxref("NodeList")}}</li>
</ul>