index.html代碼內容
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>導航高亮顯示</title> <style type="text/css"> body{ font-size:20px; } .nav{ list-style-type: none; margin:0; padding:0; } .clear:after{ content:'/20'; display:block; clear:both; height:0; visibility: hidden; } .nav li{ float:left; background:#B1DFF5; margin-right:1px; color:#fff; } .nav li a{ display:block; height:45px; width:120px; line-height:45px; text-align:center; text-decoration:none; } .active{ background:#28b1f3; font-weight:bold; } </style></head><body> <ul class="nav clear" id="nav"> <li><a href="index.html" rel="external nofollow" rel="external nofollow" >首頁</a></li> <li><a href="1.html" rel="external nofollow" rel="external nofollow" >欄目一</a></li> <li><a href="2.html" rel="external nofollow" rel="external nofollow" >欄目二</a></li> <li><a href="3.html" rel="external nofollow" rel="external nofollow" >欄目三</a></li> </ul></body><script type="text/javascript" src="js/jquery-1.7.min.js"></script><script type="text/javascript"> var urlstr = location.href; console.log(urlstr+'/'); var urlstatus=false; $("#nav a").each(function () { if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') { $(this).addClass('active'); urlstatus = true; } else { $(this).removeClass('active'); } }); if (!urlstatus) {$("#nav a").eq(0).addClass('active'); }</script></html>1.html代碼內容
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>欄目一</title> <style type="text/css"> body{ font-size:20px; } .nav{ list-style-type: none; margin:0; padding:0; } .clear:after{ content:'/20'; display:block; clear:both; height:0; visibility: hidden; } .nav li{ float:left; background:#B1DFF5; margin-right:1px; color:#fff; } .nav li a{ display:block; height:45px; width:120px; line-height:45px; text-align:center; text-decoration:none; } .active{ background:#28b1f3; font-weight:bold; } </style></head><body><ul class="nav clear" id="nav"> <li><a href="index.html" rel="external nofollow" rel="external nofollow" >首頁</a></li> <li><a href="1.html" rel="external nofollow" rel="external nofollow" >欄目一</a></li> <li><a href="2.html" rel="external nofollow" rel="external nofollow" >欄目二</a></li> <li><a href="3.html" rel="external nofollow" rel="external nofollow" >欄目三</a></li> </ul><h1>欄目一</h1></body><script type="text/javascript" src="js/jquery-1.7.min.js"></script><script type="text/javascript"> var urlstr = location.href; console.log(urlstr+'/'); var urlstatus=false; $("#nav a").each(function () { if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') { $(this).addClass('active'); urlstatus = true; } else { $(this).removeClass('active'); } }); if (!urlstatus) {$("#nav a").eq(0).addClass('active'); }</script></html>效果圖

注意:
1、 location.href 用于獲取當前頁面的url
2、 當前頁面應該保存為index.html
3、 indexOf 用于檢索當前url中是否存在a中對應的href
4、 每個html中都擁有相同的導航結構
5、 eq(index/-index) 獲取當前鏈式操作中第N個JQuery對象,返回JQquery對象,當參數大于等于0時為正向選取,比如0代表第一個,1代表第二個。當參數為負數時為反向選取,比如-1代表倒數第一個。
拓展知識:js實現導航菜單點擊切換選中時高亮狀態方法
通過 include() 或require() 函數,您可以在服務器執行 PHP 文件之前在該文件中插入一個文件的內容。
除了它們處理錯誤的方式不同之外,這兩個函數在其他方面都是相同的。
include() 函數會生成一個警告(但是腳本會繼續執行),
而 require() 函數會生成一個致命錯誤(fatal error)(在錯誤發生后腳本會停止執行)。
這兩個函數用于創建可在多個頁面重復使用的函數、頁眉、頁腳或元素。
這會為開發者節省大量的時間。
這意味著您可以創建供所有網頁引用的標準頁眉或菜單文件。當頁眉需要更新時,您只更新一個包含文件就可以了,或者當您向網站添加一張新頁面時,僅僅需要修改一下菜單文件(而不是更新所有網頁中的鏈接)。
這時就會出現這樣的問題:導航高亮應該怎樣處理?
公共代碼提出后就不可能在每個頁面的導航欄目后加class=“active”屬性進行修改,這時就需要使用javascript來搞定。
代碼如下:
<script type="text/javascript" src="http://www.daixiaorui.com/Public/js/jquery.min.js"></script><style> .menu { padding:0; margin:0; list-style-type:none;} .menu li { background:#FFD1A4; margin-right:1px; float:left; color:#fff; } .menu li a { display:block; width:80px; text-align:center; height:32px; line-height:32px; color:#fff; font-size:13px; text-decoration:none;} .cur{ background:#D96C00; font-weight:bold;}</style> <ul class="menu" id="menu"> <li><a href="demo1.html?aa=1" rel="external nofollow" >首頁</a></li> <li><a href="demo1.html?aa=2" rel="external nofollow" >欄目一</a></li> <li><a href="demo1.html?aa=3" rel="external nofollow" >欄目二</a></li></ul> <script type="text/javascript"> var urlstr = location.href; //alert(urlstr); var urlstatus=false; $("#menu a").each(function () { if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') { $(this).addClass('cur'); urlstatus = true; } else { $(this).removeClass('cur'); } }); if (!urlstatus) {$("#menu a").eq(0).addClass('cur'); }</script>運行效果:

以上這篇使用JS實現導航切換時高亮顯示的示例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答