原生js实现tab切换 与 jq实现tab切换!

原生js实现tab切换 与 jq实现tab切换

原生js实现

**css部分** 
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .warp{
            width: 100%;
            background-color: cyan;
            display: flex;
        }
        .top{
            width: 80vh;
            height: 50px;
            margin-left: 350px;
            display: flex;
        }
        .top div{   
            background-color: cadetblue;
            width: 100vh;
            height:50px;
            line-height: 50px;
            text-align: center;
            border-right: solid black 1px;
            flex-direction: row;
        }
        h1{
            text-align: center;
        }
        .top .active{
            display: block;
            background-color: yellowgreen;
            color: white;
        }
        .bottom{
            flex: 1;
            /* background-color: palegoldenrod; */

        }
        .bottom>div{
            display: none;
            text-align: center;
        }
        .bottom .show{
            display: block;
        }
        table{
            width: 100vh;
            border-collapse: collapse;
            cursor: pointer;
            margin: 0 auto;
        }
        table td{
            text-align: center;
        }
    </style>
    <div class="wrap">
        <h1>垃圾分类</h1>
       <div class="top">
               <div  class="active">生活垃圾分类</div>
               <div>可回收物</div>
               <div>厨余垃圾</div>
               <div>其他垃圾</div>
               <div>有害垃圾</div>
        </div> 
       <div class="bottom">
                    <div class="show">
                        <h2>欢迎来到生活垃圾分类首页</h2>
                    <label>垃圾名称:</label>
                    <input type="text" name="" id="user"/>
                    <br>
                        <div class="add">
                            <button id="add1">添加到可回收物</button>
                            <button id="add2">添加到厨余垃圾</button>
                            <button id="add3">添加到其他垃圾</button>
                            <button id="add4">添加到有害垃圾</button>
                        </div>
                    </div>
     </div>
js部分
 var divs=document.querySelectorAll(".top>div")
        var divs1=document.querySelectorAll(".bottom>div")
        console.log(divs,divs1);
        for( var i=0; i<divs.length; i++){
            divs[i].index = i;
            divs[i].onclick =function(){
                for(var j=0; j<divs1.length; j++){
                    divs[j].className = "";
                    divs1[j].className ="";
                }
                this.className = "active";
                divs1[this.index].className = "show";
            }
        }

jq实现

css部分
 <style>
        *{
            margin: 0;
            padding: 0;
        }
        .warp{
           width: 100%;
        }
        ul li{
            list-style: none;
        }
        .top{
            width: 300px;
            height: 30px;
            display: flex;
        }
        .top li{
            flex-direction: row;
            width: 200px;
            height: 30px;
            line-height: 30px;
            border: solid 1px  black;
            text-align: center;
        }
        .top .active{
            background-color: paleturquoise;
            color: white;
        }
        .buttom>div{
            width: 300px;
            height: 200px;
            border: solid 1px  black;
            display: none;
        }
        .buttom .show{
            display: block;
        }
    </style>
jq部分
 <script>
         $(".top>li").on("click",function(){
             var index= $(this).index();
             $(this).addClass("active").siblings().removeClass("active");
             $(".buttom>div").eq(index).addClass("show").siblings().removeClass("show");
         })
 </script>

总而言之jq的写法相对原生js来说简化了好多! 仅供参考!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个简单的例子: HTML: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>Some text...</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>Some text...</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>Some text...</p> </div> ``` CSS: ```css /* 隐藏所有tab内容 */ .tabcontent { display: none; } /* 标签页按钮样式 */ .tab button { background-color: #eee; border: none; color: black; padding: 10px 20px; cursor: pointer; } /* 激活的标签页按钮样式 */ .tab button.active { background-color: #ccc; } /* 标签页内容样式 */ .tabcontent { padding: 20px; border: 1px solid #ccc; } /* 第一个标签页默认显示 */ #tab1 { display: block; } ``` JavaScript: ```javascript function openTab(event, tabName) { // 获取所有的 tab 内容 var tabcontent = document.getElementsByClassName("tabcontent"); // 隐藏所有的 tab 内容 for (var i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 获取所有的 tab 按钮 var tablinks = document.getElementsByClassName("tablinks"); // 将所有的 tab 按钮样式设置为非激活状态 for (var i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前选中的 tab 内容 document.getElementById(tabName).style.display = "block"; // 设置当前选中的 tab 按钮为激活状态 event.currentTarget.className += " active"; } ``` 当用户点击任意一个标签页按钮时,`openTab`函数会被调用,隐藏所有标签页内容,将当前标签页内容设置为显示状态,同时将当前标签页按钮样式设置为激活状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值