NodeList.keys()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

NodeList.keys() 方法返回 iterator ,此方法允许遍历这个对象中包含的所有的键,即使这个键是 unsigned integer(无符号整数).

语法

js
keys()

返回值

返回 iterator.

示例

js
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);
}

结果是:

0
1
2

浏览器兼容性

BCD tables only load in the browser

参见