首页 > 解决方案 > JavaScript for of 循环未迭代整个 HTMLCollection

问题描述

我正在尝试遍历列表<ul>,所以我有代码

  let frost = document.createElement("ul");
  let kiba = document.getElementsByTagName("li");
  console.log(kiba);

  for (todos of kiba) {
    console.log(todos.style.color);
    if (todos.style.color === `red`) { frost.appendChild(todos); } 
  }

  for (todos of kiba) {
    //console.log(todos);
    if (todos.style.color === `black`) {frost.appendChild(todos);} 
  }

  for (todos of kiba) {
    //console.log(todos);
    if (todos.style.color === `green`) {frost.appendChild(todos);} 
  }

  
let tasks = document.getElementById(`tasks`);
//for (i in todos) { tasks.appendChild(i); }
console.log(frost);

但它并没有达到所有的<li>,只有一些。我不知道为什么。

标签: javascript

解决方案


推荐阅读