此代码使得当用户单击其中一种颜色 (color1-4) 时,它会设置display所有鞋子的 CSS 属性,none但单击的颜色除外,其display设置为block. 代码看起来很脏,雇主不会批准。
我将如何着手制作一个 for 循环,或者以其他方式使代码更清晰?
color2.addEventListener('click', () => {
shoe.style.display = "none";
shoe3.style.display = "none";
shoe5.style.display = "none";
shoe2.style.display = "block";
console.log('u removed it and added another');
});
color3.addEventListener('click', () => {
shoe.style.display = "none";
shoe3.style.display = "none";
shoe5.style.display = "none";
shoe2.style.display = "none";
shoe3.style.display = "block";
console.log('u removed it and added another');
});
color4.addEventListener('click', () => {
shoe.style.display = "none";
shoe3.style.display = "none";
shoe5.style.display = "none";
shoe3.style.display = "none";
shoe5.style.display = "block";
console.log('u removed it and added another');
});
color1.addEventListener('click', () => {
shoe.style.display = "block";
shoe2.style.display = "none";
shoe3.style.display = "none";
shoe4.style.display = "none";
shoe5.style.display = "none";
console.log('u removed it and added another');
});
color2.addEventListener('click', () => {
shoe.style.display = "none";
shoe3.style.display = "none";
shoe5.style.display = "none";
shoe2.style.display = "block";
console.log('u removed it and added another');
});
繁花不似锦
慕桂英546537
宝慕林4294392
相关分类