html: 1 <div class="nav"> 2 <ul> 3 <li><a href="index.html">首 頁</a></li> 4 <li><a href="b.html">專家觀點(diǎn)</a></li> 5 <li><a href="c.html">課題研討</a></li> 6 <li><a href="d.html">研究報(bào)告</a></li> 7 </ul> 8 </div><!--/.nav--> jquery: 1 //除了首頁外當(dāng)前URL對(duì)當(dāng)前欄目高亮突出顯示 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 //當(dāng)前URL對(duì)當(dāng)前欄目高亮突出顯示 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 當(dāng)前URL對(duì)當(dāng)前欄目高亮突出顯示 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 } |
|