完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>星星评分</title>
<style>
* {
margin: 0;
padding: 0;
}
#text {
text-align: center;
}
#pingfen {
width: 135px;
margin: 10px auto;
height: 28px;
}
#pingfen li {
width: 27px;
float: left;
height: 28px;
cursor: pointer;
background: url(star.gif) no-repeat 0 0;
list-style: none;
}
</style>
<script type="text/javascript">
window.onload = function () {
var Data = ['很差', '较差', '一般', '推荐', '力荐'];
var oDiv = document.getElementById('pingfen');
var oLi = oDiv.getElementsByTagName('li');
var oP = oDiv.getElementsByTagName('p')[0];
var i = 0;
for (i = 0; i < oLi.length; i++) {
// alert(oLi.length);
oLi[i].index = i;
oLi[i].onmouseover = function () {
oP.style.display = 'block';
for (i = 0; i <= this.index; i++) {
oLi[i].style.background = "url(star.gif) no-repeat 0 -29px";
}
oP.innerHTML = Data[this.index];
};
oLi[i].onclick = function () {
alert('评分成功,得分' + (this.index + 1) + '分');
}
oLi[i].onmouseout = function () {
oP.style.display = 'none';
for (i = 0; i < oLi.length; i++) {
oLi[i].style.background = "url(star.gif) no-repeat 0 0";
}
};
}
}
</script>
</head>
<body>
<div id="pingfen">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div id="text">
<p style="display:none">一般</p>
</div>
</div>
</body>
</html>