我刚开始与JS一起旅行,所以请耐心等待:)
我正在构建一个小应用程序。左侧的图像存储在一个数组中。如果有人点击其中一个,我想改变它的高度,但也要将布尔值从false更改为true,以便我知道用户点击了哪一个(用于进一步的目的)。在我看来它是这样的:创建一个数组 - >将EventListener添加到item->链中,该函数选择该函数(将值更改为true) - >运行更改高度的setClick函数。那有什么不对?
我劝告任何建议。
CODEPEN http://codepen.io/finewitch/pen/ZBKMKm
var storage = new Array();
storage[0] = document.getElementById("grandFatherCh");
storage[1] = document.getElementById("grandMotherCh");
storage[2] = document.getElementById("sisterCh");
storage[3] = document.getElementById("brotherCh");
storage[4] = document.getElementById("fatherCh");
storage[5] = document.getElementById("motherCh");
storage[0].clicked = false;
storage[1].clicked = false;
storage[2].clicked = false;
storage[3].clicked = false;
storage[4].clicked = false;
storage[5].clicked = false;
for (var i=0; i<storage.length; i++){
storage[i].addEventListener("click",choosen, setClick);
console.log("clicked");
};
function choosen(){
if (storage[i].clicked == false)
{
return "stillFalse"
storage[i].clicked = true;
}
};
function setClick(){
if(storage[i].clicked === true){
return "setClickWorks"
storage[i].style.height = "400px";
}else{
console.log('failed');
}
};
答案 0 :(得分:0)
也许我错过了一些东西,但我认为你在错误的地方有退货声明吗?应该是
function choosen(){
if (storage[i].clicked == false) {
return "stillFalse"
} else {
storage[i].clicked = true;
}
}
function setClick(){
if (storage[i].clicked === true){
storage[i].style.height = "400px";
return "setClickWorks"
} else {
console.log('failed');
}
}