<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>星级评分系统</title>
    <style>
        body, div, ul, li, p {
            margin: 0;
            padding: 0;
        }

        body {
            color: #666;
            font: 12px/1.5 Arial;
        }

        ul {
            list-style-type: none;
        }

        #star {
            position: relative;
            width: 600px;
            margin: 10px auto;
        }

        #star ul, #star span {
            float: left;
            display: inline;
            height: 19px;
            line-height: 19px;
        }

        #star ul {
            margin: 0 10px;
        }

        #star li {
            float: left;
            width: 24px;
            cursor: pointer;
            text-indent: -9999px;
            background: url(http://www.fgm.cc/learn/lesson4/img/star.png) no-repeat;
        }

        #star strong {
            color: #f60;
            padding-left: 10px;
        }

        #star li.on {
            background-position: 0 -28px;
        }

        #star p {
            position: absolute;
            top: 20px;
            width: 159px;
            height: 60px;
            display: none;
            background: url(http://www.fgm.cc/learn/lesson4/img/icon.gif) no-repeat;
            padding: 7px 10px 0;
        }

        #star p em {
            color: #f60;
            display: block;
            font-style: normal;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var oStar = document.getElementById("star");
            var aLi = oStar.getElementsByTagName("li");
            var oUl = oStar.getElementsByTagName("ul")[0];
            var oSpan = oStar.getElementsByTagName("span")[1];
            var oP = oStar.getElementsByTagName("p")[0];

            var indexStar = 0 ;//记录点击时的索引值
            var aMsg = [
                "很不满意|差得太离谱,与卖家描述的严重不符,非常不满",
                "不满意|部分有破损,与卖家描述的不符,不满意",
                "一般|质量一般,没有卖家描述的那么好",
                "满意|质量不错,与卖家描述的基本一致,还是挺满意的",
                "非常满意|质量非常好,与卖家描述的完全一致,非常满意"
            ]

            for(var i = 0 ; i < aLi.length; i++){
                //记录每个星星的索引
                aLi[i].index = i;
                aLi[i].onmouseenter = function () {
                    //依次的设置星星
                    for(var i = 0 ; i < aLi.length ; i++){
                        aLi[i].className = i <= this.index ?"on" : "";
                    }
                    //设置浮动的p标签
                    oP.style.display = "block";
                    //设置定位: 根据根据每个星星的个数来计算
                    oP.style.left = (this.index+1)*this.offsetWidth + 2 + "px";
                    //console.log(aMsg[this.index].match(/(.+)\|/))
                    //设置文字内容
                    oP.innerHTML = "<em><b>"+(this.index+1)+"分</b>"+aMsg[this.index].match(/(.+)\|/)[1]+"</em>"+aMsg[this.index].match(/\|(.+)/)[1];


                }

                //注册移出事件
                aLi[i].onmouseleave = function () {
                    //清空所有的on
                    // for( var i = 0 ; i < aLi.length; i++){
                    //     aLi[i].className = "";
                    // }
                    for( var i = 0 ; i < aLi.length; i++){
                        aLi[i].className = i < indexStar ? "on" : "";
                    }
                    //关闭浮动层
                    oP.style.display = "none";
                }

                //注册点击事件
                aLi[i].onclick = function () {
                    indexStar =  this.index + 1;// 把当前的索引值存进去
                    //由于不能等于0 , 所以要加 1;
                    console.log(this.index);
                    oSpan.innerHTML = "<strong>"+indexStar+"</strong>("+aMsg[this.index].match(/\|(.+)/)[1]+")";


                }
            }




        };
    </script>
</head>
<body>
<div id="star">
    <span>点击星星就能打分</span>
    <ul>
        <li><a href="javascript:;">1</a></li>
        <li><a href="javascript:;">2</a></li>
        <li><a href="javascript:;">3</a></li>
        <li><a href="javascript:;">4</a></li>
        <li><a href="javascript:;">5</a></li>
    </ul>
    <span></span>
    <p></p>
</div>
</body>
</html>
02-23 01:20