首页 > 解决方案 > 为什么我不能将数组与类列表一起使用?

问题描述

我是 javascripts 的新手,所以我需要知道为什么我不能使用类似的代码

arr[0].classList.remove("whitecolor");

例如:

元素

  const light_text_bed = document.getElementById("light_text_bed");
  const light_icon_bed = document.getElementById("light_icon_bed");

  const light_text_living = document.getElementById("light_text_living");
  const light_icon_living = document.getElementById("light_icon_living");

数组

const Textdata = [light_text_bed,light_text_living];
const Icondata = [light_icon_bed,light_icon_living];
const Firedata = ["bedroomLight","livingAreaLight"];

手术

  var count;
  for (count = 0; count < Textdata.length; count++) {
  
    const dbRef = firebase.database().ref().child(Firedata[count]);
    dbRef.on("value", (snap) => {
      var status = JSON.stringify(snap.val());

      if (status == 1) {
        Textdata[count].innerHTML = "On";
        Textdata[0].classList.add("text-warning");
        Icondata[0].classList.add("warning");

      } else if (status == 0) {
        Textdata[count].innerHTML = "Off";
        Textdata[count].classList.add("whitecolor");
        Icondata[count].classList.add("whitecolor");
      }
    });
  }

标签: javascriptarraysdom

解决方案


推荐阅读