Jquery给当前页或者跳转后页面的导航栏添加选中后样式

2022-08-04  本文已影响0人  禾苗种树
<script>
    $(document).ready(function(){ 

        //获取url地址学习
       var curPath=window.document.location.href;  
        console.log(curPath,1);
        //获取主机地址之后的目录,如: jflow-web/index.jsp  
        var pathName=window.document.location.pathname;  
        console.log(pathName,2)
        var pos=curPath.indexOf(pathName);  
        //获取主机地址,如: http://localhost:80  
        console.log(pos,'00')
        var localhostPaht=curPath.substring(0,pos);  
        // var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
        console.log(localhostPaht,3)
         console.log(window.location.href,'ooo1')


//1.0 只用到这部分就行
     $(".float_head_list a").each(function(){ 
         $this = $(this); 
         if($this[0].href==String(window.location.href)){ 
             $this.addClass("float_li_act");    
         }else{
          
         }
     }); 

// 2.0优化-本页面刷新(url携带参数且参数发生变化)
$(document).ready(function(){ 
   
     $(".y_float_head_list a").each(function(){ 
         $this = $(this); 
        let gudingUrl = $this[0].href;
        let newallUrl = window.location.href;

         if(newallUrl.indexOf(gudingUrl)!==-1){ 
             $this.addClass("y_float_li_act"); 
         }

     }); 
}); 

}); 
</script>
上一篇下一篇

猜你喜欢

热点阅读