js选项卡效果

documentAPI:

getElementById() 返回具有指定 ID 属性值的第一个对象的一个引用

getElementsByTagName 返回带有指定标签名的对象的集合, 返回元素的顺序是它们在文档中的顺序。

 getElementsByclassName 属性  设置或返回元素的 class 属性

<body>
        <!--导航栏部分HTML代码-->
        <div class="box">
            <nav id="nav">
                <ul>
                    <li><a href="#">a</a></li>
                    <li><a href="#">b</a></li>
                    <li><a href="#">c</a></li>
                    <li><a href="#">d</a></li>
                </ul>
            </nav>
        <!--内容部分HTML代码-->
            <div id="container">
                <div class="tab" style="background-color: pink;">a部分 可根据自己的需要设置内容</div>
                <div class="tab" style="background-color: cadetblue;">b部分</div>
                <div class="tab" style="background-color: blueviolet;">c部分</div>
                <div class="tab" style="background-color: chartreuse;">d部分</div>
            </div>
        </div>
</body>
   <style>
            *{
                padding: 0;
                margin: 0;
                text-decoration: none;
            }
            .box{
                position: relative;
            }
            #nav ul{
                width: 360px;
                height: 36px;
                display: flex;
                flex-direction: row;
                margin: auto;
                background-color: rgb(231, 231, 231);
            }
            #nav ul li{
                width: 58px;
                height: 35px;
                text-align: center;
                line-height: 35px;
                list-style: none;
            }
            #nav ul li:hover{
                background-color: white;
                border-top: yellow 2px solid;
                box-sizing: border-box;
            }
            #nav ul li a{
                color: black;
            }
            #container{
                width: 360px;
                margin: auto ;
                background-color: white;
            }
            .tab{
                width: 360px;
                height: 300px;
            }
        </style>
    <script>
         var nav=document.getElementById('nav');   //返回导航栏
         console.log(nav);                         //测试
         var oNav=nav.getElementsByTagName('li');  
         console.log(oNav);                        
         var container=document.getElementById('container');
         var oDiv=container.getElementsByClassName('tab');
        //  console.log(oDiv);
        //设置条件
         for(var i=0;i<oNav.length;i++){   
             
            //index 属性可返回下拉列表中选项的索引位置
             oNav[i].index=i;
             oNav[i].onmouseover=function () {    //鼠标移入效果
                 for(var i=0;i<oNav.length;i++){
                   
                     oDiv[i].style.display="none";
                 }
              
                 oDiv[this.index].style.display="block"
             }
             for(var m=1;m<oNav.length;m++){
                  
                     oDiv[m].style.display="none";
             }
         }
    </script>

完整代码

<!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>选项卡效果</title>
        <style>
            *{
                padding: 0;
                margin: 0;
                text-decoration: none;
            }
            .box{
                position: relative;
            }
            #nav ul{
                width: 360px;
                height: 36px;
                display: flex;
                flex-direction: row;
                margin: auto;
                background-color: rgb(231, 231, 231);
            }
            #nav ul li{
                width: 58px;
                height: 35px;
                text-align: center;
                line-height: 35px;
                list-style: none;
            }
            #nav ul li:hover{
                background-color: white;
                border-top: yellow 2px solid;
                box-sizing: border-box;
            }
            #nav ul li a{
                color: black;
            }
            #container{
                width: 360px;
                margin: auto ;
                background-color: white;
            }
            .tab{
                width: 360px;
                height: 300px;
            }
        </style>
</head>
    <body>
            <!--导航栏部分HTML代码-->
            <div class="box">
                <nav id="nav">
                    <ul>
                        <li><a href="#">a</a></li>
                        <li><a href="#">b</a></li>
                        <li><a href="#">c</a></li>
                        <li><a href="#">d</a></li>
                    </ul>
                </nav>
            <!--内容部分HTML代码-->
                <div id="container">
                    <div class="tab" style="background-color: pink;">a部分 可根据自己的需要设置内容</div>
                    <div class="tab" style="background-color: cadetblue;">b部分</div>
                    <div class="tab" style="background-color: blueviolet;">c部分</div>
                    <div class="tab" style="background-color: chartreuse;">d部分</div>
                </div>
            </div>
    </body>
    <script>
         var nav=document.getElementById('nav');   //返回导航栏
         console.log(nav);                         //测试
         var oNav=nav.getElementsByTagName('li');  
         console.log(oNav);                        
         var container=document.getElementById('container');
         var oDiv=container.getElementsByClassName('tab');
        //  console.log(oDiv);
        //设置条件
         for(var i=0;i<oNav.length;i++){   
             
            //index 属性可返回下拉列表中选项的索引位置
             oNav[i].index=i;
             oNav[i].onmouseover=function () {    //鼠标移入效果
                 for(var i=0;i<oNav.length;i++){
                   
                     oDiv[i].style.display="none";
                 }
              
                 oDiv[this.index].style.display="block"
             }
             for(var m=1;m<oNav.length;m++){
                  
                     oDiv[m].style.display="none";
             }
         }
    </script>
</html>

效果图

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值