<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.active{
color: red;
}
</style>
</head>
<body>
<div class="wrapper">
<span class="active">♥</span>
<span>♥</span>
<span>♥</span>
<span>♥</span>
<span>♥</span>
</div>
<script>
let _spans=document.querySelectorAll("span");
[..._spans].forEach((_span,index)=>{
_span.onclick=function(){
for(let i=0;i<_spans.length;i++){
if(i<=index){
_spans[i].className='active';
}else{
_spans[i].className='';
}
}
}
})
</script>
</body>
</html>
js实现星级评价
于 2023-04-03 20:42:52 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)