javascript - 事件监听器在 javascript 中无法正常工作

标签 javascript arrays function for-loop button

所以我是一个 javascript 菜鸟。我正在为网页设计类(class)做作业,我们正在练习事件监听器。所以我必须将变量分配给3个单独的div,将它们放在一个数组中,创建一个函数来使它们显示或隐藏,并使用按钮/单击事件监听器来调用函数来分别调用或隐藏它们。这是我的代码:

var greenBox = document.getElementById("greenBox");
var redBox = document.getElementById("redBox");
var blueBox = document.getElementById("blueBox");
var showALL = document.getElementById("showAll");
var hideALL = document.getElementById("hideAll");

var boxes = [greenBox, redBox, blueBox];

function showBoxes(boxes) {
  for (i = 0; i < boxes.length; i++) {
    boxes[i].style.display = "block";
  }
}

function hideBoxes(boxes) {
  for (i = 0; i < boxes.length; i++) {
    boxes[i].style.display = "none";
  }
}

showALL.addEventListener("click", function() {
  showBoxes(boxes);
})


// The "Hide All" button invokes the hideBoxes method
hideALL.addEventListener("click", function() {
  hideBoxes(boxes);
})
<form action="#">
  <input type="button" id="showAll" value="Show All"><br />
  <input type="button" id="hideAll" value="Hide All"><br />
</form>

<div class="box" id="greenBox" style="display:none"></div>
<div class="box" id="redBox" style="display:none"></div>
<div class="box" id="blueBox" style="display:none"></div>

那我到底做错了什么?我已经检查了多次是否有拼写错误等,但对我来说一切看起来都不错?但它仍然不起作用。

最佳答案

document.getElementById 区分大小写。

当您搜索 document.getElementById("showALL"); 时,您会得到 id="showAll"

选择一个一致的案例,它应该可以工作。

var greenBox = document.getElementById("greenBox");
var redBox = document.getElementById("redBox");
var blueBox = document.getElementById("blueBox");
var showALL = document.getElementById("showAll");
var hideALL = document.getElementById("hideAll");


var boxes = [greenBox, redBox, blueBox];

function showBoxes(boxes) {
  for (i = 0; i < boxes.length; i++) {
    boxes[i].style.display = "block";
  }
}

function hideBoxes(boxes) {
  for (i = 0; i < boxes.length; i++) {
    boxes[i].style.display = "none";
  }
}

showALL.addEventListener("click", function() {
  showBoxes(boxes);
})


// The "Hide All" button invokes the hideBoxes method
hideALL.addEventListener("click", function() {
  hideBoxes(boxes);
})
.box {width: 200px; height: 36px;}

#redBox {background-color: #F00;}
#greenBox {background-color: #0F0;}
#blueBox {background-color: #00F;}
<form action="#">
  <input type="button" id="showAll" value="Show All"><br />
  <input type="button" id="hideAll" value="Hide All"><br />
</form>

<div class="box" id="greenBox" style="display:none"></div>
<div class="box" id="redBox" style="display:none"></div>
<div class="box" id="blueBox" style="display:none"></div>

关于javascript - 事件监听器在 javascript 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016681/

相关文章:

javascript - 如何使用 onclick 按钮重新调用生成函数

javascript - 如何以字符串格式获取javascript对象类型?

javascript - 如何从 Firefox 扩展中运行外部 jar 文件

javascript - Tap 的函数签名(K 组合器)

arrays - 以 30% 的概率动态分配和初始化新对象

c++ - 在对象数组的每个索引上调用不同的构造函数

javascript - Ionic APP 中的 Disqus

javascript - 如何从单个数组对象中的多个数组对象创建数组?

javascript - 添加到 JavaScript 函数

mysql - 根据其他字段的内容生成唯一的 MySQL 字段