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

Posted aisowe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样单独遍历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> ]
// ...

 

以上是关于怎样单独遍历NodeList的键值和键值对的主要内容,如果未能解决你的问题,请参考以下文章