tab切换,原生js

tab切换,原生js

第一种方式

    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .big {
            width: 900px;
            height: 600px;
            margin: 0 auto;
        }

        ul {
            list-style: none;
            overflow: hidden;
            width: 300px;
            height: 20px;
            margin: 0 auto;
            margin-bottom: 30px;
        }

        li {
            float: left;
            margin-left: 40px;
            cursor: pointer;
        }

        img {
            width: 800px;
            height: 500px;
            margin-left: 50px;
            display: none;

        }

        .red {
            color: pink;
        }

        .active {
            display: block;
        }
    </style>
    <body>
    <div class="big">
        <ul id="ul">
            <li class="red">图片一</li>
            <li>图片二</li>
            <li>图片三</li>
        </ul>
        <!-- 图片随便找三张尺寸大点的图片,是尺寸大点,不是尺度大点,如果是尺度大点,请私信发我看下,嘿嘿嘿 -->
        <div id="picture">
            <img src="./img/1.jpg" alt="" class="active">
            <img src="./img/2.jpg" alt="">
            <img src="./img/3.jpg" alt="">
        </div>
    </div>
    <script>
        var ul = document.getElementById('ul');
        var li = document.getElementsByTagName('li');
        var img = document.getElementsByTagName('img');
        for (var i = 0; i < li.length; i++) {
            li[i].index = i
            li[i].onclick = function () {
                for (var m = 0; m < li.length; m++) {
                    li[m].removeAttribute('class')
                    img[m].removeAttribute('class')
                }
                this.setAttribute('class', 'red')
                img[this.index].setAttribute('class', 'active')
            }
        }
    </script>
</body>

第二种方式

在这里插入图片描述

    <style>
        ul {
            overflow: hidden;
            list-style: none;
        }

        li {
            float: left;
            margin-left: 20px;
        }

        .red {
            color: red;
        }

        p {
            display: none;
        }

        .active {
            display: block;
        }
    </style>
    <body>
    <ul id="tit">
        <li class="red" onclick="tab(0)">标题1</li>
        <li onclick="tab(1)">标题2</li>
        <li onclick="tab(2)">标题3</li>
    </ul>
    <div id="con">
        <p class="active">内容一</p>
        <p>内容二</p>
        <p>内容三</p>
    </div>
    <script>
        var li = document.getElementsByTagName('li')
        var p = document.getElementsByTagName('p')
        function tab(i) {
            for (var j = 0; j < li.length; j++) {
                li[j].setAttribute('class', '')
                p[j].setAttribute('class', '')
            }
            li[i].setAttribute('class', 'red')
            p[i].setAttribute('class','active')
        }
    </script>
</body>

肯定还有其他比较好的方法,可以提出来,欢迎来搞,感谢支持!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

7cccccc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值