<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://libs.cdnjs.net/jquery/3.1.0/jquery.js"></script>
<style type="text/css">
.box{
width:300px;
height:40px;
background:red;
}
li{
height: 40px;
width:40px;
background: url('star.png');
background-repeat: no-repeat;
background-size: 25px 25px;
list-style: none;
background-position: center;
float: left;
cursor: pointer;
}
#score{
color:red;
margin-top:20px;
}
</style>
</head>
<body>
<div class="box" id="star0" score="">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</div>
<div id="score"></div>
<script type="text/javascript">
$(".box li").click(function(){
var index = $(this).index();
$(this).parent().attr("score",index+1);
var parentId = $(this).parent().attr("id");
$("#"+parentId).find("li").css("background-image","url('star.png')");
for(var i=0;i<=index;i++){
$("#"+parentId).find("li").eq(i).css("background-image","url('star_active.png')");
}
var score = $("#"+parentId).attr("score");
$("#score").text("您选择的分数是:"+score+"分");
});
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://libs.cdnjs.net/jquery/3.1.0/jquery.js"></script>
<style type="text/css">
.box{
width:300px;
height:40px;
background:red;
}
li{
height: 40px;
width:40px;
background: url('star.png');
background-repeat: no-repeat;
background-size: 25px 25px;
list-style: none;
background-position: center;
float: left;
cursor: pointer;
}
#score{
color:red;
margin-top:20px;
}
</style>
</head>
<body>
<div class="box" id="star0" score="">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</div>
<div id="score"></div>
<script type="text/javascript">
$(".box li").click(function(){
var index = $(this).index();
$(this).parent().attr("score",index+1);
var parentId = $(this).parent().attr("id");
$("#"+parentId).find("li").css("background-image","url('star.png')");
for(var i=0;i<=index;i++){
$("#"+parentId).find("li").eq(i).css("background-image","url('star_active.png')");
}
var score = $("#"+parentId).attr("score");
$("#score").text("您选择的分数是:"+score+"分");
});
</script>
</body>
</html>