解決思路,導航欄目鏈接跟當前欄目對應頁面鏈接之間做比較,相等就給對應的導航欄目所在的li元素添加樣式
html:
1 <div class="nav"> 2 <ul> 3 <li><a href="index.html">首 頁</a></li> 4 <li><a href="b.html">專家觀點</a></li> 5 <li><a href="c.html">課題研討</a></li> 6 <li><a href="d.html">研究報告</a></li> 7 </ul> 8 </div><!--/.nav-->
jquery:
1 //除了首頁外當前URL對當前欄目高亮突出顯示 2 $(".nav li a:not(:first)").each(function(){ 3 $this = $(this); 4 if($this[0].href==String(window.location)){ 5 $this.parent().addClass("selected"); 6 } 7 }); 8 //當前URL對當前欄目高亮突出顯示 9 $(".sidenav li a").each(function(){ 10 $this = $(this); 11 if($this[0].href==String(window.location)){ 12 $this.parent().addClass("selected"); 13 } 14 });
或者原生js:
1 //js 當前URL對當前欄目高亮突出顯示 2 var myNav = document.getElementById("nav").getElementsByTagName("a"); 3 for(var i=0;i<myNav.length;i++){ 4 var links = myNav[i].getAttribute("href"); 5 var myURL = document.location.href; 6 if(myURL.indexOf(links) != -1){ 7 myNav[i].parentNode.className="selected"; 8 } 9 }
jquery參考
js參考http://hi.baidu.com/pwqok/item/0e21cc14c53026ce38cb3073
