·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 原生javascript星级评分

原生javascript星级评分

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

写个最简单的原生js的星级评分:

Html代码  
  1. <div id="rank" class="pingfen">  
  2.     <ul>  
  3.         <li></li>  
  4.         <li></li>  
  5.         <li></li>  
  6.         <li></li>  
  7.         <li></li>  
  8.     </ul>  
  9.     <p>加载中</p>  
  10. </div>  

 

CSS代码  
  1. <style type="text/css">  
  2.     *{margin: 0;padding: 0;}  
  3. .pingfen{ width: 135px; margin:10px auto; height:20px; position: relative;}  
  4. .pingfen ul{height:20px; margin-bottom: 10px;}  
  5. .pingfen li{ width: 20px; float: left; height: 20px; cursor: pointer; background: url(star.png) no-repeat 0 0; list-style: none;}  
  6. .pingfen .active{background: url(star.png) no-repeat 0 -28px;}  
  7. .pingfen p{ position: absolute; top:24px; left: 0px; width: 134px; height: 28px; background: #fff; line-height: 28px; text-align: center; border:1px solid #333; display:none;}  
  8. </style>  

 

Js代码  
  1. <script>  
  2. var aData =["很差","较差","一般","推荐","力推"];  
  3.   
  4. window.onload=function(){  
  5.     var oDiv = document.getElementById("rank");  
  6.     var aLi = oDiv.getElementsByTagName("li");  
  7.     var oP = oDiv.getElementsByTagName("p")[0];  
  8.     var i =0;  
  9.     for(i=0;i<aLi.length;i++){  
  10.         aLi[i].index = i;  
  11.             aLi[i].onmouSEOver = function(){  
  12.             oP.style.display = "block";  
  13.             oP.innerHTML=aData[this.index];  
  14.             for(i=0; i<=this.index;i++){  
  15.                 aLi[i].className="active";  
  16.             }  
  17.         };  
  18.         aLi[i].onmouseout = function(){  
  19.             oP.style.display = "";  
  20.             for(i=0; i<aLi.length; i++){  
  21.                 aLi[i].className="";  
  22.             }  
  23.         };  
  24.         aLi[i].onclick=function(){  
  25.             alert(this.index +1);  
  26.         };  
  27.     }  
  28.   
  29. };  
  30. </script>  

 

 

ok超级简单不信你试试。