练习题4_history历史管理.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  12. li{
  13. list-style: none;
  14. }
  15. .box{
  16. width: 600px;
  17. height: 100px;
  18. background-color: #999;
  19. margin:100px auto;
  20. }
  21. .box ul{
  22. display: flex;
  23. }
  24. .box li{
  25. width: 200px;
  26. height: 100px;
  27. font-size: 50px;
  28. font-weight: bold;
  29. text-align: center;
  30. line-height: 100px;
  31. color: white;
  32. }
  33. .box ul .active{
  34. background-color: #111;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="box">
  40. <ul>
  41. <li class="active">first</li>
  42. <li>second</li>
  43. <li>thrid</li>
  44. </ul>
  45. </div>
  46. <script>
  47. var lis = document.querySelectorAll("li");
  48. // 循环绑定事件
  49. for(var i=0;i<lis.length;i++){
  50. lis[i].onclick = function(){
  51. for(var j=0;j<lis.length;j++){
  52. lis[j].classList.remove("active");
  53. }
  54. this.classList.add("active");
  55. }
  56. }
  57. </script>
  58. </body>
  59. </html>