jQuery选项卡案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="lib/jquery-3.5.1.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            list-style: none;
        }
        .header{
            display: flex;
            width: 500px;
        }
        .header li{
            flex:1;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px solid black;
        }
        .box{
            position: relative;
        }
        .box li{
            position: absolute;
            left: 0;
            top: 0;
            width: 500px;
            height: 200px;
            background-color: yellow;

            /* 隐藏元素并脱离文档 */
            display: none;
        }
        .header .active{
            background-color: red;
        }
        .box .active{
            /* 将行内元素转成块级元素,块级元素一般单独占一行 */
            display: block;
        }
    </style>
</head>
<body>
    <ul class="header">
        <li class="active">正在热映</li>
        <li>即将上映</li>
        <li>选择影院</li>
        <li>选择时间</li>
    </ul>
    <ul class="box">
        <li class="active">0-没有电影</li>
        <li>1-没有上映</li>
        <li>2-没有影院</li>
        <li>3-没有时间</li>
    </ul>
    <script>
        $(".header li").click(function(){
            console.log(this,$(this))
            $(this).addClass("active").siblings().removeClass()//给this加active,并且它兄弟的active移除掉
            console.log($(this).index())//拿到选项的索引值去类比下面卡片的索引值

            var index = $(this).index()
            // 1
            // $(".box li").eq(index).addClass("active").siblings().removeClass("active")
            // 2
            $(".box li").removeClass("active").eq(index).addClass("active")
        })
    </script>
</body>
</html>

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值