第一篇web学习文章-jQuery【案例-五星好评】

没有正经的写过文章,第一次写,如有错误请斧正。

本次主要是练习了jQuery的简单用法,刚开始学习使用。不多废话了,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>五星好评</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            margin: 100px auto 0;
            width: 310px;
            height: 40px;
        }

        .dd {
            margin-top: 0;
            padding-left: 10px;
            width: 300px;
        }

        p {
            width: 36px;
            height: 36px;
            border-radius: 36px;
            border: 2px solid red;
            float: left;
            margin-right: 10px;
            cursor: pointer;
        }

        .dd p {
            width: 20px;
            height: 20px;
            border-radius: 20px;
            margin: 10px 30px 10px 0;
            border: none;
        }

        span {
            float: left;
            margin-left: 20px;
            width: 36px;
            height: 36px;
            border: 2px solid #0627c6;
            text-align: center;
            color: #c91818;
            line-height: 36px;
        }
    </style>
    <script src="jquery-1.12.4.js"></script>
    <script>
        $(function () {
             //1.给p 注册鼠标经过事件,让自己和前面所有的兄弟都变成实心
            $(".comment>p").on("mouseenter", function () {
                //判断有没有dianJi这个添加的标识,第一次进入的时候没有之后,有标识后进入执行else
                if ($(this).attr("class") != "dianJi") {
                    $(this).css("backgroundColor", "red");
                    $(this).prevAll().css("backgroundColor", "red");
                    $(this).nextAll().css("backgroundColor", "");

                } else {
                    $(this).css("backgroundColor", "red");
                    if ($(this).index() + 1 == $("span").text()) {
                        $(this).nextAll().css("backgroundColor", "");
                    }

                }

            });
            //3. 给comment注册鼠标离开事件
            // 两种情况:
            //  1、没单击离开,则让所有的p都变成空心
            //  2、单击离开了,则单击的p与前边的p需要变实心
            $(".comment").on("mouseleave", function () {
            //  1、让所有的p都变成空心
                $(this).children().css("backgroundColor", "");
            //再做一件事件,找到dianJi,让dianJi变成实心就行。
                $("p.dianJi").css("backgroundColor", "red").prevAll().css("backgroundColor", "red");
            });
            //2. 给p注册点击事件
            $(".comment>p").on("click", function () {
            // 当一个p被单击时,需要添加一个dianJi类标识自己是被单击的,
            // 前面的兄弟也被标记,后面的其它的兄弟要删除此类。
                $(this).addClass("dianJi").css("backgroundColor", "red").prevAll().addClass("dianJi").css("backgroundColor", "red");
                $(this).nextAll().removeClass("dianJi").css("backgroundColor", "");

                //后面方框中的值
                $("span").text($(this).attr("class", "dianJi").index() + 1);
                //下面的跟随点
                $(".dd>p").eq($(this).index()).css("backgroundColor", "red").prevAll().css("backgroundColor", "");
                $(".dd>p").eq($(this).index()).nextAll().css("backgroundColor", "");
            });

        });
    </script>
</head>
<body>
<div class="comment">
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <span></span>
</div>
<div class="dd">
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>
</body>
</html>

运行效果如下:

3cece7cc127e4f4aaf10ae860aa5d476.png

9cfd328ec9ad4d6890dfe453077fd63b.png 

 代码写完,自我感觉有些冗余,目前还在优化中......

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值