自定义属性选项卡

/1. 首先清空所有div li 的 选中样式 2.给当前传递的索引 加入active样式/

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>选项卡</title>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. }
  13. ul,ol{
  14. list-style: none;
  15. }
  16. .box{
  17. width: 500px;
  18. margin: 20px auto;
  19. }
  20. .box .tab{
  21. display: flex;
  22. justify-content: flex-start;
  23. align-items: center;
  24. position: relative;
  25. top:1px
  26. }
  27. .box .tab li{
  28. box-sizing: border-box;
  29. padding: 0 20px;
  30. height: 35px;
  31. line-height: 33px;
  32. border: 1px solid #CCC;
  33. background-color: #ddd;
  34. color:#555;
  35. font-size: 14px;
  36. margin-right: 15px;
  37. cursor: pointer;
  38. }
  39. .box .tab li.active{
  40. background-color:#fff;
  41. border-bottom-color: #fff;
  42. font-weight: bold;
  43. }
  44. .box .content div{
  45. height: 200px;
  46. box-sizing: border-box;
  47. padding: 14px;
  48. border: 1px solid #CCC;
  49. display: none;
  50. }
  51. .box .content div.active{
  52. display: block;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <section class="box" id="box">
  58. <ul class="tab">
  59. <li class="active">音乐</li>
  60. <li>电影</li>
  61. <li>动漫</li>
  62. </ul>
  63. <div class="content">
  64. <div class="active">认真的雪</div>
  65. <div>我的姐姐</div>
  66. <div>斗罗大陆</div>
  67. </div>
  68. </section>
  69. <script>
  70. /*
  71. document.getElementById([元素ID]);//元素对象
  72. [context].getElementsByTagName([标签名]);//通过元素名获得一组元素集合
  73. [context].getElementsByClassName([样式类名]);通过类名获得一组元素集合 不支持ie6/7/8
  74. document.getElementsByName([name属性值]); 例如checkbox
  75. document.documentElement 获取HTML元素对象
  76. document.head 获取HEAD元素对象
  77. documnet.body 获取BODY元素对象
  78. ------
  79. 不兼容IE 6 7 8
  80. [context].querySelector([选择器]) 获取和选择器匹配中的第一个对象
  81. [context].querySelectorAll([选择器]) 获取选择器匹配的所有结果 【节点集合 类数组】
  82. className 获取或设置标签的class类名
  83. */
  84. var box=document.querySelector("#box"),
  85. tabList=box.querySelectorAll(".tab>li"),
  86. conList=box.querySelectorAll('.content>div');
  87. /* 封装一个方法 告诉我点击是哪个 点击那一项的索引传递进来
  88. 我首先把所有的LI/DIV 都清除选中样式,然后再让告诉的这个有选中样式
  89. */
  90. function changeTab(index){
  91. for (var i = 0; i < tabList.length; i++) {
  92. tabList[i].className='';
  93. conList[i].className='';
  94. }
  95. tabList[index].className='active';
  96. conList[index].className='active';
  97. }
  98. /* 循环所有的li 分别给每一个Li绑定点击事件 当点击某个LI的时候 执行changeTab 并且传递对应索引 */
  99. for (var i = 0; i < tabList.length; i++) {
  100. tabList[i].myIndex=i;
  101. tabList[i].onmouseover=function(){
  102. changeTab(this.myIndex);
  103. };
  104. };
  105. </script>
  106. </body>
  107. </html>

选项卡优化

/
1.给当前的li和div 增加active选中 清空上一个active选中的样式
/

<!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>
        *{
            margin: 0;
            padding: 0;
        }

        ul,ol{
            list-style: none;
        }

        .box{
            width: 500px;
            margin: 20px auto;
        }

        .box .tab{
            display: flex;
            justify-content: flex-start;
            align-items: center;
            position: relative;
            top:1px
        }

        .box .tab li{
            box-sizing: border-box;
            padding: 0 20px;
            height: 35px;
            line-height: 33px;
            border: 1px solid #CCC;
            background-color: #ddd;
            color:#555;
            font-size: 14px;
            margin-right: 15px;
            cursor: pointer;
        }

        .box .tab li.active{
            background-color:#fff;
            border-bottom-color: #fff;
            font-weight: bold;
        }

        .box .content div{
            height: 200px;
            box-sizing: border-box;
            padding: 14px;
            border: 1px solid #CCC;
            display: none;
        }

        .box .content div.active{
            display: block;
        }

    </style>
</head>
<body>
    <section class="box" id="box">
        <ul class="tab">
            <li class="active">音乐</li>
            <li>电影</li>
            <li>动漫</li>
        </ul>
        <div class="content">
            <div class="active">认真的雪</div>
            <div>我的姐姐</div>
            <div>斗罗大陆</div>
        </div>
    </section>

    <script>
        var box=document.querySelector("#box"),
            tabList=box.querySelectorAll(".tab>li"),
            conList=box.querySelectorAll('.content>div');
        //prevIndex 上一个选中LI的索引 初始值为零 因为最开始选中的是第一个
        var prevIndex=0;
        for (var i = 0; i < tabList.length; i++) {
            tabList[i].myIndex=i;
            tabList[i].onmouseover=function(){
                //this.myIndex:当前点击这一项的索引
                var curindex=this.myIndex;
                if(curindex===prevIndex)return;
                //4. 如果当前操作的这一项和选中的这一项是同一项 则无需做任何处理
                //1. 操作的是谁 就让谁有选中样式
                //className: 存储的是字符串,代表当前元素的类名
                tabList[curindex].className=conList[curindex].className='active'; 

                //之前选中的是谁 让他没有选中样式
                tabList[prevIndex].className=conList[prevIndex].className='';

                //当前操作的索引是下一次操作的”上一次索引“
                prevIndex=curindex;
            };
        };

    </script>
</body>
</html>
<!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>
        *{
            margin: 0;
            padding: 0;
        }

        ul,ol{
            list-style: none;
        }

        .box{
            width: 500px;
            margin: 20px auto;
        }

        .box .tab{
            display: flex;
            justify-content: flex-start;
            align-items: center;
            position: relative;
            top:1px
        }

        .box .tab li{
            box-sizing: border-box;
            padding: 0 20px;
            height: 35px;
            line-height: 33px;
            border: 1px solid #CCC;
            background-color: #ddd;
            color:#555;
            font-size: 14px;
            margin-right: 15px;
            cursor: pointer;
        }

        .box .tab li.active{
            background-color:#fff;
            border-bottom-color: #fff;
            font-weight: bold;
        }

        .box .content div{
            height: 200px;
            box-sizing: border-box;
            padding: 14px;
            border: 1px solid #CCC;
            display: none;
        }

        .box .content div.active{
            display: block;
        }

    </style>
</head>
<body>
    <section class="box" id="box">
        <ul class="tab">
            <li class="active">音乐</li>
            <li>电影</li>
            <li>动漫</li>
        </ul>
        <div class="content">
            <div class="active">认真的雪</div>
            <div>我的姐姐</div>
            <div>斗罗大陆</div>
        </div>
    </section>

    <script>
        var box=document.querySelector("#box"),
            tabList=box.querySelectorAll(".tab>li"),
            conList=box.querySelectorAll('.content>div');
        //prevIndex 上一个选中LI的索引 初始值为零 因为最开始选中的是第一个
        var prevIndex=0;
        for (var i = 0; i < tabList.length; i++) {
            tabList[i].myIndex=i;
            tabList[i].onmouseover=function(){
                //this.myIndex:当前点击这一项的索引
                var curindex=this.myIndex;
                if(curindex===prevIndex)return;
                //4. 如果当前操作的这一项和选中的这一项是同一项 则无需做任何处理
                //1. 操作的是谁 就让谁有选中样式
                //className: 存储的是字符串,代表当前元素的类名
                tabList[curindex].className=conList[curindex].className='active'; 

                //之前选中的是谁 让他没有选中样式
                tabList[prevIndex].className=conList[prevIndex].className='';

                //当前操作的索引是下一次操作的”上一次索引“
                prevIndex=curindex;
            };
        };

    </script>
</body>
</html>