当我在元素之外单击时,如何更改星级并将其设为默认值?
另外,例如,如果我点击第四颗星,我的前4颗星是红色的,但如果我再次点击第二颗星,我应该只会得到第一和第二颗红色,第三和第四颗之前的蓝色。
let stars=document.querySelector(".hearts").children;
for(let i = 0; i<stars.length; i++){
stars[i].onmouseover = funcion;
stars[i].onmouseout = funcionn;
stars[i].addEventListener('click', funcio3);
function funcion (){
for(let j= 0; j<=i; j++){
stars[j].classList.remove('fa-heart');
stars[j].classList.add('fa-w-16');
}
};
function funcionn (){
for(let j= 0; j<=i; j++){
stars[j].classList.remove('fa-w-16');
stars[j].classList.add('fa-heart');
}
};
function funcio3 (){
for(let j= 0; j<=i; j++){
stars[j].style.color = 'red';
}
};
};发布于 2020-04-22 20:56:30
document.addEventListener('click', evt => {
if (!evt.target.closest('.hearts')) {
// click has been fired outside .hearts children
}
});
https://stackoverflow.com/questions/61364021
复制相似问题