正确的代码来了
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
span {
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload = function () {
// 获取元素
var stars = document.getElementById('stars').getElementsByTagName('span');
var ret = document.getElementById('ret');
function getStar(e){
var b=e.getAttribute('star')
for(var j=0;j<b;j++){
stars[j].style.color="red";
}
ret.innerHTML=j+'个红星';
}
for(var i=0;i<stars.length;i++){
stars[i].onclick=function(e){
getStar(e.srcElement);
}
}
}
</script>
</head>
<body>
<div id="stars">
<span star="1">★</span>
<span star="2">★</span>
<span star="3">★</span>
<span star="4">★</span>
<span star="5">★</span>
</div>
<div id="ret"></div>
</body>
</html>