5.选项卡.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * {
  9. margin: 0;
  10. padding: 0;
  11. list-style: none;
  12. text-decoration: none;
  13. box-sizing: border-box;
  14. }
  15. #container {
  16. width: 482px;
  17. margin: 190px auto;
  18. border: 1px solid #000;
  19. }
  20. #container #list {
  21. overflow: hidden;
  22. }
  23. #container #list li {
  24. float: left;
  25. width: 120px;
  26. height: 50px;
  27. text-align: center;
  28. line-height: 50px;
  29. border: 1px solid #ccc;
  30. }
  31. .active {
  32. width: 480px;
  33. height: 300px;
  34. text-align: center;
  35. line-height: 300px;
  36. font-size: 25px;
  37. color: purple;
  38. font-weight: 600;
  39. border: 1px solid #f00;
  40. display: none;
  41. }
  42. .selected {
  43. background: #f00;
  44. color: #ff0;
  45. }
  46. .choose {
  47. display: block;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="container">
  53. <ul id="list">
  54. <li class="selected">用户管理</li>
  55. <li>配置管理</li>
  56. <li>角色管理</li>
  57. <li>定时任务补</li>
  58. </ul>
  59. <div id="main">
  60. <div class="choose active">用户管理</div>
  61. <div class="active">配置管理</div>
  62. <div class="active">角色管理</div>
  63. <div class="active">定时任务补</div>
  64. </div>
  65. </div>
  66. <script>
  67. var btn = document.querySelectorAll("#list li");
  68. var contain = document.querySelectorAll(".active");
  69. console.log(this,'this1')
  70. for(var i=0;i<btn.length;i++) {
  71. // 为了给每一个按钮添加点击事件
  72. // 下标赋值
  73. btn[i].index = i;
  74. btn[i].onclick = function() {
  75. console.log(this,'this2')
  76. // 2.点击 恢复样式
  77. for(var j=0;j<contain.length;j++) {
  78. // 4次
  79. btn[j].className = '';
  80. contain[j].className = 'active';
  81. }
  82. // this => 当前对象 = btn[i]
  83. // 添加selected样式
  84. // 1.点击 改变颜色
  85. this.className = 'selected';
  86. // 添加choose样式
  87. contain[this.index].className = 'choose active';
  88. }
  89. }
  90. </script>
  91. </body>
  92. </html>