怎样单独遍历NodeList的键、值和键值对

1. 单独遍历键: NodeList.prototype.keys();

2. 单独遍历值: NodeList.prototype.values();

3. 遍历键值对: NodeList.prototype.entries();

 

var children = document.body.childNodes;

for (var key of children.keys()) {
  console.log(key);
}
// 0
// 1
// 2
// ...

for (var value of children.values()) {
  console.log(value);
}
// #text
// <script>
// ...

for (var entry of children.entries()) {
  console.log(entry);
}
// Array [ 0, #text ]
// Array [ 1, <script> ]
// ...

 

posted on 2019-09-12 16:05  aisowe  阅读(1119)  评论(0编辑  收藏  举报

导航