JS导航栏的选中问题

管理员 1274 次浏览

摘要: JS导航栏的选中问题,这边有两种情况, 一种是直接给当前页面添加特殊样式,当网页刷新或者跳转到下一页后,样式消失; 另一种情况是即使刷新页面后样式仍然有效。

博客来源于:讯杰设计网:http://blogs.51diysoft.com/blogs

  1. 这边有两种情况,

  2. 一种是直接给当前页面添加特殊样式,当网页刷新或者跳转到下一页后,样式消失;

  3. 另一种情况是即使刷新页面后样式仍然有效。直接上代码。


    1、直接给当前页面添加特殊样式

    [html] 

    在CODE上查看代码片派生到我的代码片


  4. <script type="text/JavaScript" src="templets/js/jquery.js"></script>  
    <script type="text/javascript">  
    $(document).ready(function(){  
        $(".nav a").each(function(){  
            $(this).click(function(){  
                $(".nav .hover").removeClass("hover");  
                $(this).addClass("hover");    
                return false;//防止页面跳转  
            });  
        });  
    });  
    </script>  
      
    <div class="nav">  
    <ul>  
        <li><a href="1.html" class="hover"> 首页</a></li>  
        <li><a href="2.html"> 个人资料</a></li>  
        <li><a href="3.html"> 我的好友</a></li>  
        <li><a href="4.html"> 消息管理</a></li>   
    </ul>     
    </div>
  5. 2、另一种情况是即使刷新页面后样式仍然有效

  6. <script type="text/javascript" src="templets/js/jquery.js"></script>  

  7. <script type="text/javascript">  

  8. $(document).ready(function(){  

  9.     $(".nav a").each(function(){  

  10.       var navURL=this.href;

  11.         if(navURL==window.location.href){  

  12.             $this.addClass("hover");  

  13.         }  

  14.     });  

  15. });  

  16. </script>  

  17.   

  18. <div class="nav">  

  19. <ul>  

  20.     <li><a href="1.html"> 首页</a></li>  

  21.     <li><a href="2.html"> 个人资料</a></li>  

  22.     <li><a href="3.html"> 我的好友</a></li>  

  23.     <li><a href="4.html"> 消息管理</a></li>   

  24. </ul>     

  25. </div>