1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- ul{
- margin: 0;
- padding: 0;
- display: flex;
- }
- li{
- list-style: none;
- width: 100px;
- height: 50px;
- background-color: #aaa;
- text-align: center;
- line-height: 50px;
- font-weight: bold;
- color: #fff;
- }
- .active{
- background-color: #000;
- }
- </style>
- </head>
- <body>
- <ul>
- <li class="active">fist</li>
- <li>second</li>
- <li>third</li>
- </ul>
- <script>
- let aLi = document.querySelectorAll("li");
- let arr = ['first','second','third'];
- for(let i = 0;i<aLi.length;i++){
- // aLi[i].index = i;
- aLi[i].onclick = function(){
- for(let j=0;j<aLi.length;j++){
- aLi[j].className = "";
- }
- this.className = "active";
- // console.log(i);
- location.hash = arr[i];
- // console.log(arr[this.index]);
- // location.hash = arr[this.index]
- }
- }
- window.onhashchange = function(){
- // console.log(location.hash)
- let thisHas = location.hash.substr(1);
- // console.log(thisHas);
- // console.log(arr.indexOf(thisHas));
- let thisIndex = arr.indexOf(thisHas)
- for(let i =0;i<aLi.length;i++){
- aLi[i].className = "";
- }
- aLi[thisIndex].className = "active";
- }
- </script>
- </body>
- </html>
|