Feb 22

js+css结合高亮导航当前页链接 不指定

admin , 16:45 , 默认分类 , 评论(28) , 引用(0) , 阅读(5457) , Via 本站原创 | |

  js+css结合高亮导航当前页链接

  很多朋友在做网站的时候可能遇到这样一个问题,如何高亮导航当前页链接 
  以育儿网新版导航条来举例
  大家可以看到“首页”2个字做了高亮处理。 

  <div id=”navi”>
          <ul>
              <li><a target=”_blank” title=”首页” href=”http://www.ci123.com/” class=”on” ><span>首页</span></a></li>
              <li><a target=”_blank” title=”父母学堂” href=”http://www.ci123.com/index1.html”><span>父母学堂</span></a></li>
              <li><a target=”_blank” title=”育儿论坛” href=”http://bbs.ci123.com”><span>育儿论坛</span></a></li>
              <li><a target=”_blank” title=”宝宝主页” href=”http://baobao.ci123.com”><span>宝宝主页</span></a></li>
              <li><a target=”_blank” title=”育儿博客” href=”http://blog.ci123.com”><span>育儿博客</span></a></li>
              <li><a target=”_blank” title=”育儿问答” href=”http://ask.ci123.com”><span>育儿问答</span></a></li>
              <li><a target=”_blank” title=”许愿树” href=”http://tree.ci123.com”><span>许愿树</span></a></li>
              <li><a target=”_blank” title=”亲子资源” href=”http://rs.ci123.com”><span>亲子资源</span></a></li>
              <li><a target=”_blank” title=”城市伙伴” href=”http://www.ci123.com/baodian”><span>育儿宝典</span></a></li>
              <li class=”end”><a target=”_blank” title=”活动吧” href=”http://www.ci123.com/huod-bar.html”><span>活动吧</span></a></li>
          </ul>
      </div> 

  蓝色加下划线的地方,就是用来区别“首页”这个链接和其它链接的不同样式的class。

  #navi li a.on,#navi li a:hover{
      background: url(images/navi.gif) top left no-repeat;
      color: Red;
  }#navi li a.on  span,#navi li a:hover span{
      background: url(images/navi.gif);
          background-position: right 25px;
      padding-top: 2px;

  }a 里面的 span 标签是为了实现滑动门效果的。具体可以看CSS滑动门技术这篇文章。下面继续。如果是单个页面,设置这样的效果只要按上面的方法,在需要高亮度链接标识出特定的class就可以了。但在大型网站中,因为频道和页面众多,如果每个频道的头部都为此单独去做一个只是高亮部位不同的文件,那维护起来必然是非常繁琐的。因此,我们将头部分离出来,放入服务器单独的目录,再让各个频道去引用这个单独的文件。这样在维护的时候,就可以一次修改。多次使用。具体引用的方式在不同的语言和架构上有不同的方法,这个和我们要说的东西又是另一回事。回到这里,如果是引用的同一个头部文件,那么就没有办法给每个链接区按一个class,那又如何按频道高亮呢。下面有个函数:function highlightPage(){
      if(!document.getElementsByTagName) return false;
      if(!document.getElementById) return false;
      if(!document.getElementById(’navi’)) return false;
      var nav = document.getElementById(’navi’);
      var links = nav.getElementsByTagName(’a');
      for (var i=0;i<links.length;i++){
          var linkurl =  links[i].getAttribute(’href’);
          var currenturl = document.location.href;
              if(currenturl.indexOf(linkurl)!=-1){
                  links[i].className = ’on’;
                  return true;
              }
      }

  }这个js的前三行,是用于判断dom上的节点是否能取到,以及浏览器对document.getElementsById方法的支持程度。 

  到了第三行开始,建立了一个 nav的变量,并给它赋值document.getElementById(’navi’)。 

  navi是唯一的。而它下面的诸多a 标签,用links[]这个数组再进行赋值。 

  赋值完成后,就开始遍历。一遍links的数组,每遍历一个数组成员,都用 links[i].getAttribute(’href’) 将这个成员的’href’属性值取出,放入临时变量linkurl,同时用l将页面当前的地址也取出放入currenturl这个变量。 

  如http://www.ci123.com/  因为基本网页的实际地址长度都会大于页面代码href的里面所写的路径,所以我们用indexOf这个方法来判断 currenturl 里面是否包含 linkurl的字符串,如果结果为true,我们就给这个链接的className赋值’on’ 设定它为当前链接。然后返回。 

  这样开头的那个高亮效果就实现了。

   

  -----------------------------------------------------------------------------------------------

  <script>
  function addloadEvent(fun){
      var olonload=window.onload;
      if(typeof window.onload!='function'){
          window.onload=fun;
      }else{
          window.onload=function(){
              olonload();
              fun();
          }
      }
  }
  function highlightpage(){
      if(!document.getElementsByTagName) return false;
      if(!document.getElementById) return false;
      if(!document.getElementById("navigation")) return false;
      var nav=document.getElementById("navigation");
      var links = nav.getElementsByTagName("a");
      for(var i=0;i<links.length;i++){
          var linkurl=links[i].getAttribute("href");
          var currenturl= window.location.href;
         if(currenturl.indexOf(linkurl)!=-1){
                   var a=linkurl.lastIndexOf("/")+1
                  var b=linkurl.lastIndexOf(".")
                  var c=linkurl.substring(a, b)
              links[i].className=c;
          }
      }
  }
  addloadEvent(highlightpage);
  </script>
  <style>
  a.nava:{}
  a.navb:visited{color:red}
  a.navc:{}
  a.navd:{}
  </style>
  <div id="navigation">
  <ul>
  <li><a href="nava.htm">111</a></li>
  <li><a href="navb.htm">222</a></li>
  <li><a href="navc.htm">333</a></li>
  <li><a href="navd.htm">444</a></li>
  </ul>
  </div>

TabithaMontgomery Email Homepage
2012/01/29 06:49
Would you like to get <a href="http://www.prime-resume.com">resume services</a>, that conform the field of study you wish?. You can trust our resume writers, as you trust yourself. Thanks because that’s the good way
lkooj80eb Email Homepage
2012/01/28 01:38
I say this not to diminish what Tebow did Sunday. The touchdown pass on the first play of overtime after so much time had been spent on his passing deficiencies was one of the most iconic moments of the season. It is one of those great playoff moments that people will talk about years from now. It is where hype met results.,chloe bag
It is not unlike the entire Tebow phenomenon. Whatever you think of his long-term quarterbacking potential, it is indisputable that Tebowmania was refreshing. He is what we love about sports. He was inspirational. He made you believe. It is sad actually how many were unable just to enjoy the ride and instead turned everyday on a referendum on religion and Tebow's longevity when the truth is we do not know.
HintonCrystal Email Homepage
2012/01/26 02:04
In the essay writing service is simple to find information and essay writing close to this post . To do much easier your academic career buy essay and feel the free time!
ShelbyMccoy32 Email Homepage
2012/01/26 01:08
To get information just about this good post, the students buy term paper or custom writing at the term paper writing services. A lot of essays writing services render the essay writing about this good topic.
dfrmmesro45 Email Homepage
2012/01/24 01:42
Barbour granted pardons and other reprieves in his final days before leaving office after two terms Tuesday. Five inmates who had worked as trusties at the Governor's Mansion -- four of them convicted of murder -- were released last weekend. One of the freed men had fatally shot his estranged wife as she held their baby in 1993 and then shot her male friend in the head; the friend survived.
Barbour initially declined to comment on the pardons or provide detailed information about how many of those receiving them were still in prison. He then issued a statement after leaving
Lea30Moses Email Homepage
2012/01/24 00:24
Want to find free tv essays paper? That is not a problem, just check out services, which propose the free essays.
KristiBrennan Email Homepage
2012/01/07 13:58
I guess that the colossal base of the thesis paper about this topic is at the buy thesis service. Thus, there’re no difficulties to run to dissertation service and buy theses.
BARKERCecile33 Email Homepage
2012/01/07 11:55
I do opine that this is doable to visit this page, because only here we will find the fantastic issue related to this post. So, the buy dissertation service could utilize it for model dissertation composing.
TurnerTara30 Email Homepage
2011/12/31 14:31
You can feel the strength of professional approach. Power, teamwork, quality! That is how we do optimization work at our seo packages service. You can be sure that not a single thing is putted beyond our hark and your site will be well optimized by the best specialists!
Katharine20Flowers Email Homepage
2011/11/26 06:57
Inexperienced students usually opt for the first research papers writing corporation they find online. It is wrong! You should order <a href="http://www.bestwritingservice.com/research-paper-writing1.html">research paper writing</a> utilizing expert tips.
分页: 1/3 第一页 1 2 3 下页 最后页
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]