我有一组按钮被选中,并添加了带有for循环的事件侦听器,当单击每个按钮时,我想切换它的样式,并且还将与单击的按钮相关的变量设置为true或false。能够在for循环中切换其样式,但是有没有办法在同一循环中切换该变量,而不是分别向每个按钮添加事件侦听器? 我的代码是:
document.getElementById("orderalert1").addEventListener("click", function () {
m = !(m != false) })
document.getElementById("orderalert2").addEventListener("click", function () {
l = !(l != false) })
document.getElementById("orderalert3").addEventListener("click", function () {
k = !(k != false) });
document.getElementById("orderalert4").addEventListener("click", function () {
j = !(j != false) });
document.getElementById("orderalert5").addEventListener("click", function () {
n = !(n != false) });
document.getElementById("orderalert6").addEventListener("click", function () {
o = !(o != false) });
var is=document.querySelectorAll(".select");
function toggle(){for(var i = 0; i < is.length; i++){ is[i].addEventListener("click", function(){
this.classList.toggle("done"); }); }}
toggle();