# 菜單高亮顯示
我們在實際項目中,經常使用到“菜單高亮”這個功能,這個看似簡單的功能,卻難倒了絕大多數的前端,有的人用前端實現,有的人用后端實現,甚至有時候用很多方法都不奏效!
> 我認為:用jquery下面的這種方法,我認為是最好的!
## 一、原理
1、在菜單層的鏈接加上一個 `rel` 屬性,保存 `href` 屬性即可,
2、然后利用瀏覽器地址欄中的 `URL` 和 `rel` 對比,
3、如果相等就在當前的 `<a>`標簽加上 `class` ,同時移除其它 `<a>` 標簽的 `class`。
## 二、代碼段
```
<script type="text/javascript">
var urlstr = location.href; //獲取瀏覽器的url
var urlstatus = false; //標記
//遍歷導航div
$(".dropdown a").each(function() {
//判斷導航里面的rel和url地址是否相等
if((urlstr + '/').indexOf($(this).attr('rel')) > -1 && $(this).attr('rel') != '') {
$(this).addClass('cur');
urlstatus = true;
} else {
$(this).removeClass('cur');
}
});
//當前樣式保持
if(!urlstatus) {
$(".dropdown a").eq(0).addClass('cur');
}
</script>
```
## 三、示例
具體代碼,如圖所示:

1、index.html 和 index_b.html 的代碼相同,如下:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css" />
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="script.js"></script>
<style>
.cur{background: #EF9B11;color:#FF0000;}
</style>
</head>
<body>
<ul>
<li class="dropdown">
<a id="a" href="index.html" class="dropbtn" onclick="showList(this)" rel="index.html">標題 A</a>
<div class="dropdown-content" id="dropdown-a">
<a href="#">下拉 1</a>
<a href="#">下拉 2</a>
<a href="#">下拉 3</a>
</div>
</li>
<li class="dropdown">
<a id="b" href="index_b.html" class="dropbtn" onclick="showList(this)" rel="index_b.html">標題 B</a>
<div class="dropdown-content" id="dropdown-b">
<a href="#">下拉 1</a>
<a href="#">下拉 2</a>
<a href="#">下拉 3</a>
</div>
</li>
</ul>
<script type="text/javascript">
var urlstr = location.href; //獲取瀏覽器的url
var urlstatus = false; //標記
//遍歷導航div
$(".dropdown a").each(function() {
//判斷導航里面的rel和url地址是否相等
if((urlstr + '/').indexOf($(this).attr('rel')) > -1 && $(this).attr('rel') != '') {
$(this).addClass('cur');
urlstatus = true;
} else {
$(this).removeClass('cur');
}
});
//當前樣式保持
if(!urlstatus) {
$(".dropdown a").eq(0).addClass('cur');
}
</script>
</body>
</html>
```
2、css代碼
```
body {
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #1f75cf;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #fafafa;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
color: white;
background-color: #1f75cf;
}
.show {
display: block;
}
```
3、JS代碼
```
function showList(o) {
hideList("dropdown-content" + o.id);
document.getElementById("dropdown-" + o.id).classList.toggle("show");
}
function hideList(option) {
var dropdowns = document.getElementsByClassName("dropdown-content");
for (var i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.id != option) {
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {
hideList("");
}
}
```
- 前言
- 第一章 基礎知識
- 圖片上傳
- 第二章 功能模塊數據字典
- 1.1門戶管理
- 門戶管理模塊
- term_relationships 文章分類對應表
- terms 文章分類表
- posts 文章表
- 1.2課程系統
- 課程系統模塊
- card 點卡管理
- cardtype 點卡類型
- application 申請教師
- course 課程管理
- coursetype 課程分類
- label 標簽管理
- material 資料列表
- order 訂單管理
- section 課時管理
- teacher_order 教師審核
- tixian 提現
- usercourse 用戶課程
- users 用戶表
- 1.3家庭檔案(健康家)
- 家庭檔案模塊
- family_files 家庭檔案表
- file_classify 家庭檔案分類表
- users_health 健康家用戶
- 1.4商城系統
- 商城系統模塊
- 商城配置
- 商品-訂單模塊
- 2.1系統設置
- 系統設置模塊
- option 全站配置表
- 3.1用戶管理
- 用戶管理模塊
- users 用戶表
- oauth_user 第三方用戶表
- user_favorites 用戶收藏表
- common_action_log 點贊記錄表
- 3.2RBAC管理
- RBAC管理模塊
- role 角色表
- role_user 用戶角色對應表
- auth_access 權限授權表
- auth_rules 權限規則表
- 3.3菜單管理
- 菜單模塊
- menu 后臺菜單表
- nav 前臺導航表
- nav_cate 前臺導航分類表
- route URL路由表
- 4.1評論管理
- 評論管理模塊
- comments 評論表
- 4.2幻燈管理
- 幻燈管理模塊
- slide 幻燈片表
- slide_cat 幻燈片分類表
- 4.3友情鏈接模塊
- links 友情鏈接表
- 4.4廣告管理模塊
- ad 廣告表
- 4.5留言管理
- guestbook 留言表
- 4.6 微信管理模塊
- wx_user 微信用戶
- wx_keyword 關鍵詞
- wx_menu 微信菜單
- wx_text 文本回復
- wx_img 微信圖文
- wx_news 圖文消息?
- 5.1插件管理模塊
- 插件模塊
- plugins 插件表
- district 中國省市區鄉鎮數據表
- 5.2擴展工具
- asset 資源表
- famous 名人名言表
- 區域管理
- region 區域管理
- 第三章 系統模塊
- 門戶系統
- 教學系統
- 博客系統
- 醫療系統
- 商城系統
- 一站式檢測系統
- 第四章 視頻教程
- 4.1 NewThink視頻教程
- 4.2 ApiCloud視頻教程
- 4.3 ThinkPHP視頻教程
- 4.4 JQuery視頻教程
- 4.5 PHP視頻教程
- 4.6 IOS視頻教程
- 第五章 PHP加解密
- 5.1 PHP解密
- 5.2 PHP加密
- 第六章 后臺菜單
- 第七章 前端模塊
- 下拉刷新
- 輪播特效
- 獲得焦點時-加黑色蒙版動畫
- 獲得焦點時-圖片抖動動畫
- 導航下拉隱藏上拉顯示
- 菜單高亮顯示
- 商品大菜單
- 六宮格
- 第八章 工具類
- 相關知識
- 附件一:nav.php
- git沖突的解決辦法