zoukankan      html  css  js  c++  java
  • js练习 选项卡效果

    选项卡内容以背景色代替,选中的选项背景色变灰并显示对应的选项卡

    <html>
    <head>
    <meta charset="utf-8">
    <title>选项卡效果</title>
    <style type="text/css">
        *{ margin:0 auto; padding:0;}
        #wai{ width:1000px; height:500px; margin-top:50px;}
        #btl{width:350px; height:30px; margin-left:50px;}
        .bt{ width:50px; height:30px; text-align:center; line-height:30px; float:left;}
        .bt:hover{ cursor:pointer;}
        .nr{ width:100%; height:450px; display:none;}
    </style>
    </head>
    
    <body>
        <div id="wai">
            <div id="btl">
                <div class="bt" sy="0" style="background-color:#999">国内</div>
                <div class="bt" sy="1">国际</div>
                <div class="bt" sy="2">军事</div>
                <div class="bt" sy="3">财经</div>
                <div class="bt" sy="4">社会</div>
                <div class="bt" sy="5">娱乐</div>
                <div class="bt" sy="6">体育</div>
            </div>
            <div class="nr" style="background-color:#F00; display:block"></div>
            <div class="nr" style="background-color:#0F0"></div>
            <div class="nr" style="background-color:#F60"></div>
            <div class="nr" style="background-color:#00F"></div>
            <div class="nr" style="background-color:#FF0"></div>
            <div class="nr" style="background-color:#0FF"></div>
            <div class="nr" style="background-color:#F0F"></div>
        </div>
    </body>
    </html>
    <script type="text/javascript">
        var bt = document.getElementsByClassName("bt");
        var nr = document.getElementsByClassName("nr");
        /*
            选中的标题变色,其他标题恢复原色,选中标题的对应选项卡显示,其他选项卡隐藏
        */
        for(var i=0;i<bt.length;i++)
        {
            bt[i].onclick = function()
            {
                for(var i=0;i<bt.length;i++)
                {
                    bt[i].style.backgroundColor = "#fff";
                }
                this.style.backgroundColor = "#999";
                var syh = this.getAttribute("sy");
                for(var i=0;i<nr.length;i++)
                {
                    nr[i].style.display = "none";
                }
                nr[syh].style.display = "block";
            }
        }
        
    
    </script>

    效果

  • 相关阅读:
    暴躁游戏

    时间记录表格
    好好生活
    JAVA环境的配置
    Java简介
    markdown学习

    Arduino
    Arduino
  • 原文地址:https://www.cnblogs.com/zxbs12345/p/8004398.html
Copyright © 2011-2022 走看看