3_选项卡.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. </style>
  39. </head>
  40. <body>
  41. <div id="container">
  42. <ul id="btn">
  43. <li class="selected">时事</li>
  44. <li>新闻</li>
  45. <li>体育</li>
  46. </ul>
  47. <div id="div1">
  48. <div>时事内容</div>
  49. <div>新闻内容</div>
  50. <div>体育内容</div>
  51. </div>
  52. </div>
  53. <script>
  54. var btn = document.getElementById('btn')
  55. var btns = btn.getElementsByTagName('li')
  56. for(var i=0;i<btns.length;i++){
  57. btns[i].onclick = function(){
  58. //this 点谁就是谁
  59. for(var j=0;j<btns.length;j++){
  60. btns[j].className = ''
  61. }
  62. this.className = 'selected'
  63. }
  64. }
  65. </script>
  66. </body>
  67. </html>