<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>星级评分1.1</title>
<style>
body,li,ul {
padding: 0;
margin: 0;
}
li {
list-style-type: none;
}
.rating {
width: 500px;
height: 460px;
margin: 100px auto;
}
.rating-item {
float: left;
width: 100px;
height: 92px;
background: url('light.jpg')no-repeat;
cursor: pointer;
}
</style>
</head>
<body>
<ul class="rating" id="rating">
<li class="rating-item" title="很差"></li>
<li class="rating-item" title="差"></li>
<li class="rating-item" title="一般"></li>
<li class="rating-item" title="好"></li>
<li class="rating-item" title="很好"></li>
</ul>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
// console.log($);
var num = 2,
$rating = $('#rating'),
$item =$rating.find('.rating-item');
//点亮
var lightOn = function (num) {
$item.each(function (index) {
if (index < num) {
$(this).css('background-position', '0 0px');
} else {
$(this).css('background-position', '0 -100px');
}
});
};
//初始化
lightOn(nun);
//事件绑定
$item.on('mouseover',function(){
lightOn($(this).index()+1);
}).on('click',function(){
num = $(this).index()+1;
});
$rating.on('mouseout',function(){
lightOn(num);
});
</script>
</body>
</html>
你运行的时候不打开控制台么?
打开控制台就能看到你错哪了,你那个
//初始化
lightOn(nun);
你传参传错了是num