今天又用了一下淘宝评价,发现淘宝评价不是鼠标移上去变化,而是直接用手点击。所以把前两天的代码又修改了一下,改成点击选中。
第一种实现方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>淘宝五星评分</title>
<style>
#mark{width: 360px;}
#mark img{width: 24px;height: 24px;}
.pic{font-size: 24px;}
.float_r{float: right;}
</style>
</head>
<body>
<div id="wrap">
<div id="mark">
<div id="pic1" class="pic" onmouseover="comments(1)">
<strong>描述相符</strong>
<img src="img/starw.png" name="star1" data-index="1">
<img src="img/starw.png" name="star2" data-index="2">
<img src="img/starw.png" name="star3" data-index="3">
<img src="img/starw.png" name="star4" data-index="4">
<img src="img/starw.png" name="star5" data-index="5">
<span id="txt1"> </span>
</div>
<div id="pic2" class="pic" onmouseover="comments(2)">
<strong>物流服务</strong>
<img src="img/starw.png" name="star1" data-index="1">
<img src="img/starw.png" name="star2" data-index="2">
<img src="img/starw.png" name="star3" data-index="3">
<img src="img/starw.png" name="star4" data-index="4">
<img src="img/starw.png" name="star5" data-index="5">
<span id="txt2"> </span>
</div>
<div id="pic3" class="pic" onmouseover="comments(3)">
<strong>服务态度</strong>
<img src="img/starw.png" name="star1" data-index="1">
<img src="img/starw.png" name="star2" data-index="2">
<img src="img/starw.png" name="star3" data-index="3">
<img src="img/starw.png" name="star4" data-index="4">
<img src="img/starw.png" name="star5" data-index="5">
<span id="txt3"></span>
</div>
<br>
<button type="button" class="float_r" onClick="submit()">提交评论</button>
</div><!--end mark-->
</div>
</body>
<script type="text/javascript">
function comments(num){
var pic = document.getElementById("pic"+num);
var imgList = pic.getElementsByTagName("img");
var comments=["非常差","差","一般","好","非常好"];
var txt= document.getElementById("txt"+num);
for(var i=0;i<imgList.length;i++){
imgList[i].onclick= function(){
var index = this.getAttribute("data-index");
for(var j=0;j<index;j++){
imgList[j].setAttribute("src","img/starred.png");
} //index之前的红色星
for(var k=index;k<imgList.length;k++){
imgList[k].setAttribute("src","img/starw.png");
} //index之后的白色星
txt.innerHTML=comments[index-1];
}
} //end for
} //end function comments
function submit(){
document.write('谢谢您的评价!');
}
</script>
</html>
第二种实现方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 淘宝五星评分click版</title>
<style type="text/css">
img{height: 24px;width: 24px;}
body{font-size: 24px;}
</style>
</head>
<body>
<div id="pic1">
<span>描述相符:</span>
<img data-index='1' src="img/starw.png" >
<img data-index='2' src="img/starw.png" >
<img data-index='3' src="img/starw.png" >
<img data-index='4' src="img/starw.png" >
<img data-index='5' src="img/starw.png" >
<span></span>
</div>
<div id="pic2">
<span>物流服务:</span>
<img data-index='1' src="img/starw.png" >
<img data-index='2' src="img/starw.png" >
<img data-index='3' src="img/starw.png" >
<img data-index='4' src="img/starw.png" >
<img data-index='5' src="img/starw.png" >
<span></span>
</div>
<div id="pic3">
<span>服务态度:</span>
<img data-index='1' src="img/starw.png" >
<img data-index='2' src="img/starw.png" >
<img data-index='3' src="img/starw.png" >
<img data-index='4' src="img/starw.png" >
<img data-index='5' src="img/starw.png" >
<span></span>
</div>
</body>
<script type="text/javascript">
var divs=document.querySelectorAll('div');
for(var i=0;i<divs.length;i++){ //监听div
divs[i].addEventListener('mouseover',over);
}
function over(){ //监听img
imgs=this.querySelectorAll('img'); //把var去掉,全局变量
for(var k=0;k<imgs.length;k++){
imgs[k].addEventListener('click',checked);
}
}
function checked(){
var index=this.getAttribute('data-index');
var comments=["非常差","差","一般","好","非常好"];
for(var j=0;j<index;j++){
imgs[j].src='img/starred.png';
}
for(var j=index;j<imgs.length;j++){
imgs[j].src='img/starw.png';
}
this.parentNode.lastElementChild.innerHTML=comments[index-1];
}
</script>
</html>