zoukankan      html  css  js  c++  java
  • hash 哈希 锚点链接

    window.location.hash 既可以设置也可以获取
    获取的 hash   长这样->    /#p=13  。
    获取 hash 值 :hashNum = window.location.hash.split ( " = " ) [ 1 ] * 1 ;
    设置 hash 值 :window.location.hash = " p = 13 " ;
     
    hash的选项卡练习:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    <style>
    .active{
        background: yellow;
    }
    </style>
    </head>
    <body>
        <div id="box"></div>
        <p id="p"></p>
    <script>
        
        let arr = ['新闻','体育','军事','头条'];
        let btns = document.getElementsByTagName('button');
        let hashNum;//为了对应btn的颜色
        let hash = window.location.hash; //一上来先获取hash
        if(!hash){//如果没有
            window.location.hash = 'p=0'; //强行设置一波0,这个时候直接就跑到了hashchange中
        }else{
            //如果有,就获取hash值
            hashNum = window.location.hash.split('=')[1]*1;
            p.innerHTML = arr[hashNum];
        }
    
        arr.forEach((e,i)=>{
            let btn = document.createElement('button');
            btn.innerHTML = e;
            btn.className = hashNum!=undefined && (e==arr[hashNum]?"active":'');
            btn.onclick = function(){
                // for(let i=0;i<btns.length;i++){
                //     btns[i].className = ''; 
                // }
                // this.className = 'active';
                // p.innerHTML = e;
                window.location.hash = 'p='+i;
            }
            box.appendChild(btn)
        });
    
        window.onhashchange = function(){
            for(let i=0;i<btns.length;i++){
                btns[i].className = ''; 
            }
            let i = window.location.hash.split('=')[1]*1;
            btns[i].className = 'active';
            p.innerHTML = arr[i];
        }
    
    </script>
    </body>
    </html>
  • 相关阅读:
    redis在centos7下安装(源码编译)
    Centos7之Gcc安装
    Jmeter工具之上传图片,上传音频文件接口
    什么是系统平均负载(Load average)
    sonar+Jenkins 构建代码质量自动化分析平台
    数据库主从相关配置参数说明
    现有数据库配置主从同步
    MySQL5.7多主一从(多源复制)同步配置
    MySQL5.7主从从配置
    MySQL5.7主从同步配置
  • 原文地址:https://www.cnblogs.com/MrZhujl/p/10152603.html
Copyright © 2011-2022 走看看