<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
var clickId = -1;
onload = function () {
var imgs = document.getElementsByTagName('img');
for (var i = 0; i < imgs.length; i++) {
imgs[i].onmouseover = function (e) {
//获得当前焦点的Id
var id = parseInt(this.id.charAt(this.id.length - 1));
for (var j = 0; j < imgs.length; j++) {
if (j <= id) {
imgs[j].src = 'star2.png';
} else {
imgs[j].src = 'star1.png';
}
}
};
imgs[i].onmouseleave = function () {
for (var j = 0; j < imgs.length; j++) {
if (j <= clickId && clickId >= 0) {
imgs[j].src = 'star2.png';
} else {
imgs[j].src = 'star1.png';
}
}
};
imgs[i].onclick = function () {
clickId = parseInt(this.id.charAt(this.id.length - 1));
}
};
};
</script>
</head>
<body>
<img id="img0" src="star1.png" />
<img id="img1" src="star1.png" />
<img id="img2" src="star1.png" />
<img id="img3" src="star1.png" />
<img id="img4" src="star1.png" />
</body>
</html>