jquery实现评星+评价

看图:
在这里插入图片描述


DIV:

 <div class="stars">
                                            <span onmouseover="setStar(0, this)" onmouseout="outStar(0,this)"
                                                  onclick="clickStar(this)"></span>
                                            <span onmouseover="setStar(1,this)" onmouseout="outStar(1,this)"
                                                  onclick="clickStar(this)"></span>
                                            <span onmouseover="setStar(2,$(this))" onmouseout="outStar(2,this)"
                                                  onclick="clickStar(this)"></span>
                                            <span onmouseover="setStar(3,$(this))" onmouseout="outStar(3,this)"
                                                  onclick="clickStar(this)"></span>
                                            <span onmouseover="setStar(4,$(this))" onmouseout="outStar(4,this)"
                                                  onclick="clickStar(this)"></span>
                                        </div>
                                        <div class="info" >优秀</div>
                                    </div>

CSS:

<style type="text/css">
    .content {
        width: 100%;
        height: 72px;
    }

    .stars {
        width: 100%;
        list-style: none;
        margin: 0;
        line-height: 67px;
        color: #ccc;
        /*margin-left: 30px;*/
        display: flex;
        float: left;
    }

    .stars span {
        font-size: 30px;
        margin-left: 10px;
        color: red;
    }

    .info {
        float: left;
        width: 60px;
        height: 30px;
        /*background: #e15671;*/
        border-radius: 5px;
        color: red;
        margin-top: 21px;
        text-align: center;
        line-height: 30px;
        /*display: none;*/
    }


</style>

JS:

  var grades = ["极差", "差", "一般", "好", "非常好"];

    function outStar(num, obj) {
        let nextAll = $(obj).nextAll(); //该节点后所有节点
        for (let i = 0; i < nextAll.length; i++) {
                $(nextAll[i]).text("☆")
        }
    }

    function setStar(nub, obj) {
        let prevAll = $(obj).prevAll(); //该节点后所有节点
        let nextAll = $(obj).nextAll(); //该节点后所有节点
        for (let i = 0; i < nextAll.length; i++) {
            $(nextAll[i]).text("☆")
        }
        for (var i = 0; i < prevAll.length; i++) {
            $(prevAll[i]).text("★")
        }
        $(obj).text("★")
        $(obj).parent().next().text(grades[nub]);
    }

    function clickStar(obj) {
        $(obj).text("★")
    }
原创文章 63 获赞 48 访问量 8万+

猜你喜欢

转载自blog.csdn.net/csp_6666/article/details/105573033