导航条点击当前页面变色

来源:互联网 发布:km算法 c 代码实现 编辑:程序博客网 时间:2024/05/22 07:07
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<style>
.nav li.action{color:#0C9;}
</style>
</head>
<body>
<ul class="nav">
  <li ><a href="nav.html" class="first" id="page1">首页</a></li>
  <li ><a href="demo.html" id="page2">我页面</a></li>
  <li ><a href="goods.html" id="page3">赛事专栏</a></li>
  <li ><a href="jianding.html" id="page4">新闻心</a></li>
  <li ><a href="#" id="page5">兴趣心</a></li>
  <li ><a href="#" class="last" id="page6">寻找资源</a></li>
</ul>
<script>
 $(function(){

    /*当前页面导航高亮*/
    var href = window.location.href.split('/')[window.location.href.split('/').length-1].substr(0,4);
    if(href.length > 0){
        $(function(){
            $("ul.nav a:first[href^='"+href+"']").parent().attr("class","action");
            if($("ul.nav a:first[href^='"+href+"']").size() == 0){
                $("ul.nav a:first[href^='index']").parent().attr("class","action");
            }
        });
    }else{
        $(function(){$("ul.nav a:first[href^='index']").parent().attr("class","action")});
    }
 });
</script>
</body>
</html>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<style>
.nav li.action{color:#0C9;}
</style>
</head>
<body>
<ul class="nav">
  <li ><a href="nav.html" class="first" id="page1">首页</a></li>
  <li ><a href="demo.html" id="page2">我页面</a></li>
  <li ><a href="goods.html" id="page3">赛事专栏</a></li>
  <li ><a href="jianding.html" id="page4">新闻心</a></li>
  <li ><a href="#" id="page5">兴趣心</a></li>
  <li ><a href="#" class="last" id="page6">寻找资源</a></li>
</ul>
<script>
 $(function(){

    /*当前页面导航高亮*/
    var href = window.location.href.split('/')[window.location.href.split('/').length-1].substr(0,4);
    if(href.length > 0){
        $(function(){
            $("ul.nav a:first[href^='"+href+"']").parent().attr("class","action");
            if($("ul.nav a:first[href^='"+href+"']").size() == 0){
                $("ul.nav a:first[href^='index']").parent().attr("class","action");
            }
        });
    }else{
        $(function(){$("ul.nav a:first[href^='index']").parent().attr("class","action")});
    }
 });
</script>
</body>
</html>


0 0
原创粉丝点击