通過設置cookie來保存樹形菜單的狀態,在頁面加載時重新讀取cookie來設置菜單。
菜單的HTML結構:
<div class="treemenu"> <ul> <li> <a href="#" id="treemenu_a_1">一級菜單一</a> <div class="submenu" id="submenu_1"> <ul> <li><a href="subpage/a.html" id="submenu_a_1_1">二級菜單一</a></li> <li><a href="subpage/b.html" id="submenu_a_1_2">二級菜單二</a></li> <li><a href="#" id="submenu_a_1_3">二級菜單三</a></li> <li><a href="#" id="submenu_a_1_4">二級菜單四</a></li> <li><a href="#" id="submenu_a_1_5">二級菜單五</a></li> </ul> </div> </li> <li> <a href="#" id="treemenu_a_2">一級菜單二</a> <div class="submenu" id="submenu_2"> <ul> <li> <a href="#" id="submenu_a_2_1">二級菜單一</a> <div class="submenu" id="submenu_2_1"> <ul> <li><a href="#" id="submenu_a_2_1_1">三級菜單一</a></li> <li><a href="#" id="submenu_a_2_1_2">三級菜單二</a></li> <li> <a href="#" id="submenu_a_2_1_3">三級菜單三</a> <div class="submenu" id="submenu_2_1_3"> <ul> <li><a href="#" id="submenu_a_2_1_3_1">四級菜單一</a></li> <li><a href="#" id="submenu_a_2_1_3_2">四級菜單二</a></li> <li><a href="#" id="submenu_a_2_1_3_3">四級菜單三</a></li> </ul> </div> </li> </ul> </div> </li> <li><a href="#" id="submenu_a_2_2">二級菜單二</a></li> <li><a href="#" id="submenu_a_2_3">二級菜單三</a></li> <li><a href="#" id="submenu_a_2_4">二級菜單四</a></li> <li><a href="#" id="submenu_a_2_5">二級菜單五</a></li> </ul> </div> </li> <li> <a href="#" id="treemenu_a_3">一級菜單三</a> <div class="submenu" id="submenu_3"> <ul> <li><a href="#" id="submenu_a_3_1">二級菜單一</a></li> <li><a href="#" id="submenu_a_3_2">二級菜單二</a></li> <li><a href="#" id="submenu_a_3_3">二級菜單三</a></li> <li><a href="#" id="submenu_a_3_4">二級菜單四</a></li> <li><a href="#" id="submenu_a_3_5">二級菜單五</a></li> </ul> </div> </li> </ul> </div>
讀取cookie工具類:
//cookie工具類 var cookieTool = { //讀取cookie getCookie: function(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) { c_start = c_start + c_name.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) { c_end = document.cookie.length; } return unescape(document.cookie.substring(c_start, c_end)); } } return ""; }, //設置cookie setCookie: function(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); //設置日期 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); }, //刪除cookie delCookie: function(c_name) { var exdate = new Date(); exdate.setDate(exdate.getDate() - 1); //昨天日期 document.cookie = c_name + "=;expires=" + exdate.toGMTString(); } };
菜單事件綁定:
//菜單事件綁定 $('.treemenu a').bind('click', function() { var $this = $(this); var id = $this.attr('id'); var $submenu = $this.next('.submenu'); if ($submenu.length > 0) { //是否有子菜單 var flag = $(this).next('.submenu:hidden').length > 0 ? true : false; if (flag) { $submenu.show(); } else { $submenu.hide(); } var display = flag ? 'block' : 'none'; cookieTool.setCookie(id, display, 10); } else { cookieTool.setCookie(id, id, 10); var curId = cookieTool.getCookie(id); $('.treemenu').find('.on').removeClass('on').addClass('off'); $('#' + curId).addClass('on'); $('.treemenu a[class="off"]').each(function() { cookieTool.delCookie($(this).attr('id')); //刪除其他已選擇選項cookie }); } });
頁面加載時重新設置菜單
//頁面加載讀取cookies $('.treemenu a').each(function() { showMenu($(this).attr('id')); }); //讀取cookie顯示菜單 function showMenu(id) { var $this = $('#' + id); var cookie = cookieTool.getCookie(id); if (cookie) { if ($this.next('.submenu').length > 0) { $this.next('.submenu').css('display', cookie); } else { $('#' + cookie).addClass('on'); } } }
完整DEMO:
【JavaScript】刷新不變化樹形菜單(多級菜單).zip
注意:chrome本地控制台無法讀取cookie,需要在firefox/IE或者服務器環境下測試