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("★")
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值