3_选项卡.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #btn li {
  20. width: 50px;
  21. height: 30px;
  22. background: aquamarine;
  23. text-align: center;
  24. line-height: 30px;
  25. border: 1px solid #CCC;
  26. border-radius: 10px;
  27. float: left;
  28. }
  29. #btn .selected{
  30. background: orange;
  31. color: white;
  32. }
  33. #div1{
  34. width: 350px;
  35. height: 200px;
  36. border: 1px solid #ccc;
  37. }
  38. .actived{
  39. display: none;
  40. }
  41. .choice{
  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="actived choice">时事内容</div>
  55. <div class="actived">新闻内容</div>
  56. <div class="actived">体育内容</div>
  57. </div>
  58. </div>
  59. <script>
  60. var btn = document.getElementById('btn')
  61. var btns = btn.getElementsByTagName('li')
  62. var content = document.getElementsByClassName('actived')
  63. console.log(content)
  64. for(var i=0;i<btns.length;i++){
  65. btns[i].index = i
  66. btns[i].onclick = function(){
  67. //this 点谁就是谁
  68. for(var j=0;j<btns.length;j++){
  69. btns[j].className = ''
  70. content[j].className = 'actived'
  71. }
  72. this.className = 'selected'
  73. console.log(this.index)
  74. content[this.index].className = 'actived choice'
  75. }
  76. }
  77. </script>
  78. </body>
  79. </html>