他开发人员,
我想知道您对如何更好地编写此代码的看法。
如果你点击按钮star3,它会变成一个黄色的星星,下面的星星也是黄色的。star3 上方的星星只是灰色的。
这就是我得到的和我需要的,但我想知道如何在我的代码中更有效率..
var star = document.getElementById("star-0");
var star1 = document.getElementById("star-1");
var star2 = document.getElementById("star-2");
var star3 = document.getElementById("star-3");
var star4 = document.getElementById("star-4");
var img = 'url(img/star.png)';
var img2 = 'url(img/star2.png)';
star4.addEventListener('click', function(){
star.style.backgroundImage = img2;
star1.style.backgroundImage = img2;
star2.style.backgroundImage = img2;
star3.style.backgroundImage = img2;
star4.style.backgroundImage = img2;
});
star3.addEventListener('click', function(){
star.style.backgroundImage = img2;
star1.style.backgroundImage = img2;
star2.style.backgroundImage = img2;
star3.style.backgroundImage = img2;
star4.style.backgroundImage = img;
});
star2.addEventListener('click', function(){
star.style.backgroundImage = img2;
star1.style.backgroundImage = img2;
star2.style.backgroundImage = img2;
star3.style.backgroundImage = img;
star4.style.backgroundImage = img;
});
star1.addEventListener('click', function(){
star.style.backgroundImage = img2;
star1.style.backgroundImage = img2;
star2.style.backgroundImage = img;
star3.style.backgroundImage = img;
star4.style.backgroundImage = img;
});
star.addEventListener('click', function(){
star.style.backgroundImage = img2;
star1.style.backgroundImage = img;
star2.style.backgroundImage = img;
star3.style.backgroundImage = img;
star4.style.backgroundImage = img;
});