3 回答
TA贡献1851条经验 获得超4个赞
您需要使用 Array 和自定义函数来操作 DOM 元素,这是一个基本示例:
var shoes = [s1,s2,s3]; //DOM element arrays
colorShoe = (shoe) =>{
for(let i=0; i<shoes.length;i++){
if(shoes[i] === shoe){
//Handling style
}
}
TA贡献1848条经验 获得超10个赞
如果你不想使用 array ,你可以这样做。只需使用 window['var name'+the index number] 然后你可以在后面添加你想做的事情 :D 谢谢 :D
color3.addEventListener('click', () => {
for (var i = 1; i <= 5; i++) {
if (i == 3) shoe.style.display = "block";
else {
window['shoe'+i].style.display = "none";
}
}
console.log('u removed it and added another');
});
color4.addEventListener('click', () => {
for (var i = 1; i <= 5; i++) {
if (i == 4) shoe.style.display = "block";
else {
window['shoe'+i].style.display = "none";
}
}
console.log('u removed it and added another');
});
color1.addEventListener('click', () => {
for (var i = 1; i <= 5; i++) {
if (i == 1) shoe.style.display = "block";
else {
window['shoe'+i].style.display = "none";
}
}
console.log('u removed it and added another');
});
TA贡献2021条经验 获得超8个赞
请检查一下,看看您是否可以像下面这样修改您的代码:
const colorList = [color1, color2, color3]; // you can add more
const shoeList = [shoe1, shoe2, shoe3]; // you can add more
[color1, color2, color3].forEach((color, colorIndex) => {
color.addEventListener('click', () => {
shoeList.forEach((shoe, shoeIndex) => {
if (colorIndex === shoeIndex) {
shoe.style.display = "block";
} else {
shoe.style.display = "none";
}
});
});
});
添加回答
举报