练习5_TAB标签页.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. body,ul{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. li{
  13. list-style: none;
  14. }
  15. .clearfix::after{
  16. content: "";
  17. display: block;
  18. clear: both;
  19. }
  20. .box{
  21. width: 600px;
  22. height: 300px;
  23. border:1px solid black;
  24. margin: 100px auto;
  25. }
  26. .nav-content{
  27. height: 50px;
  28. background-color: black;
  29. }
  30. .nav-content li{
  31. float: left;
  32. color: #fff;
  33. padding: 0 20px;
  34. height: 50px;
  35. line-height: 50px;
  36. cursor: pointer;
  37. }
  38. .nav-content .active{
  39. background-color: #aaa;
  40. color: black;
  41. }
  42. .info-content{
  43. padding: 20px;
  44. }
  45. .info-content > div{
  46. display: none;
  47. }
  48. .info-content .active{
  49. display: block;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="box">
  55. <div class="nav-content">
  56. <ul class="clearfix">
  57. <li class="active">第一节课</li>
  58. <li >第二节课</li>
  59. <li>第三节课</li>
  60. </ul>
  61. </div>
  62. <div class="info-content">
  63. <div class="active">
  64. 第一节课内容
  65. 第一节课内容
  66. 第一节课内容
  67. 第一节课内容
  68. </div>
  69. <div class="">
  70. 第二节课内容
  71. 第二节课内容
  72. 第二节课内容
  73. 第二节课内容
  74. </div>
  75. <div class="">
  76. 第三节课内容
  77. 第三节课内容
  78. 第三节课内容
  79. 第三节课内容
  80. </div>
  81. </div>
  82. </div>
  83. <script>
  84. var liBtn = document.getElementsByTagName("li");
  85. var contentArr = document.getElementsByClassName("info-content")[0].getElementsByTagName("div");
  86. console.log(contentArr)
  87. // 循环绑定鼠标移入事件
  88. for(var i=0;i<liBtn.length;i++){
  89. // 创建新属性保持索引值
  90. liBtn[i].index = i;
  91. liBtn[i].onmouseenter = function(){
  92. // 循环清除所有标签按钮的选中状态
  93. for(var j=0;j<liBtn.length;j++){
  94. liBtn[j].setAttribute("class","");
  95. contentArr[j].setAttribute("class","");
  96. }
  97. // 仅给当前按钮加入选中状态
  98. this.setAttribute("class","active");
  99. contentArr[this.index].setAttribute("class","active");
  100. }
  101. }
  102. </script>
  103. </body>
  104. </html>