JQuery五星评价案例

演示效果

演示效果

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>五星评分案例</title>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script>
        $(function () {
            //给所有li加事件
            $(".container>ul>li").mouseover(function () {
               $(this).text("★").prevAll().text("★").end().nextAll().text("☆");
            }).mouseout(function () {   //移出来所有变回空心五角星
                $(".container>ul>li").text("☆");
                //有属性index的li保持不变
                $(".container>ul>li[index=10]").text("★").prevAll().text("★");
            }).click(function () {  //点击后保存星星
                //使用attr()方法给被点的li加一个自定义属性index
                $(this).attr("index","10").siblings().removeAttr("index");
                var ind=$(this).index();   //获取当前li的下标
                switch (ind+1) {
                    case 1:$("#score").html("极差");
                        break;
                    case 2:$("#score").html("差");
                        break;
                    case 3:$("#score").html("良");
                        break;
                    case 4:$("#score").html("好");
                        break;
                    case 5:$("#score").html("非常好");
                        break;
                }
            });
        });
    </script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .container{
            width: 300px;
            height: 40px;
            position: relative;
            margin: 50px auto;
            line-height: 40px;
            font-size: 20px;
        }
        .container ul{
            list-style: none;
        }
        .container ul li{
            float: left;
            font-size: 30px;
            color: #e6b64c;
        }
    </style>
</head>
<body>
<div class="container">
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <span id="score">请评分</span>
</div>
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值