3_选项卡.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. ul {
  14. list-style: none;
  15. }
  16. #btn {
  17. overflow: hidden;
  18. }
  19. li {
  20. width: 50px;
  21. height: 30px;
  22. border: 1px solid #ccc;
  23. text-align: center;
  24. line-height: 30px;
  25. border-radius: 10px;
  26. background: aqua;
  27. float: left;
  28. }
  29. #div1 {
  30. width: 400px;
  31. height: 200px;
  32. border: 1px solid #ccc;
  33. }
  34. .selected {
  35. color: white;
  36. background: orange;
  37. }
  38. .active {
  39. display: none;
  40. }
  41. .select {
  42. display: block;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="container">
  48. <ul id="btn">
  49. <li class="selected">时事</li>
  50. <li>新闻</li>
  51. <li>体育</li>
  52. </ul>
  53. <div id="div1">
  54. <div class="active select">时事内容</div>
  55. <div class="active">新闻内容</div>
  56. <div class="active">体育内容</div>
  57. </div>
  58. </div>
  59. <script>
  60. var btns = document.getElementsByTagName('li')
  61. var content = document.getElementsByClassName('active')
  62. for (var i = 0; i < btns.length; i++) {
  63. btns[i].index = i
  64. console.log(btns[i].index)
  65. btns[i].onclick = function () {
  66. for (var j = 0; j < btns.length; j++) {
  67. btns[j].className = ' '
  68. content[j].className = 'active'
  69. }
  70. // console.log(this.index)
  71. //谁的点击事件就是谁 btn[i].onclick btns[]
  72. this.className = 'selected'
  73. //content[索引值] = content[btns点击谁的索引值] =content[this.index]
  74. content[this.index].className = 'active select'
  75. }
  76. }
  77. </script>
  78. </body>
  79. </html>