练习题3_历史管理.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. ul{
  9. margin: 0;
  10. padding: 0;
  11. display: flex;
  12. }
  13. li{
  14. list-style: none;
  15. width: 100px;
  16. height: 50px;
  17. background-color: #aaa;
  18. text-align: center;
  19. line-height: 50px;
  20. font-weight: bold;
  21. color: #fff;
  22. }
  23. .active{
  24. background-color: #000;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <ul>
  30. <li class="active">fist</li>
  31. <li>second</li>
  32. <li>third</li>
  33. </ul>
  34. <script>
  35. let aLi = document.querySelectorAll("li");
  36. let arr = ['first','second','third'];
  37. for(let i = 0;i<aLi.length;i++){
  38. // aLi[i].index = i;
  39. aLi[i].onclick = function(){
  40. for(let j=0;j<aLi.length;j++){
  41. aLi[j].className = "";
  42. }
  43. this.className = "active";
  44. // console.log(i);
  45. location.hash = arr[i];
  46. // console.log(arr[this.index]);
  47. // location.hash = arr[this.index]
  48. }
  49. }
  50. window.onhashchange = function(){
  51. // console.log(location.hash)
  52. let thisHas = location.hash.substr(1);
  53. // console.log(thisHas);
  54. // console.log(arr.indexOf(thisHas));
  55. let thisIndex = arr.indexOf(thisHas)
  56. for(let i =0;i<aLi.length;i++){
  57. aLi[i].className = "";
  58. }
  59. aLi[thisIndex].className = "active";
  60. }
  61. </script>
  62. </body>
  63. </html>