通过设置cookie来保存树形菜单的状态,在页面加载时重新读取cookie来设置菜单。
菜单的HTML结构:
1 <div class="treemenu"> 2 <ul> 3 <li> 4 <a href="#" id="treemenu_a_1">一级菜单一</a> 5 <div class="submenu" id="submenu_1"> 6 <ul> 7 <li><a href="subpage/a.html" id="submenu_a_1_1">二级菜单一</a></li> 8 <li><a href="subpage/b.html" id="submenu_a_1_2">二级菜单二</a></li> 9 <li><a href="#" id="submenu_a_1_3">二级菜单三</a></li> 10 <li><a href="#" id="submenu_a_1_4">二级菜单四</a></li> 11 <li><a href="#" id="submenu_a_1_5">二级菜单五</a></li> 12 </ul> 13 </div> 14 </li> 15 <li> 16 <a href="#" id="treemenu_a_2">一级菜单二</a> 17 <div class="submenu" id="submenu_2"> 18 <ul> 19 <li> 20 <a href="#" id="submenu_a_2_1">二级菜单一</a> 21 <div class="submenu" id="submenu_2_1"> 22 <ul> 23 <li><a href="#" id="submenu_a_2_1_1">三级菜单一</a></li> 24 <li><a href="#" id="submenu_a_2_1_2">三级菜单二</a></li> 25 <li> 26 <a href="#" id="submenu_a_2_1_3">三级菜单三</a> 27 <div class="submenu" id="submenu_2_1_3"> 28 <ul> 29 <li><a href="#" id="submenu_a_2_1_3_1">四级菜单一</a></li> 30 <li><a href="#" id="submenu_a_2_1_3_2">四级菜单二</a></li> 31 <li><a href="#" id="submenu_a_2_1_3_3">四级菜单三</a></li> 32 </ul> 33 </div> 34 </li> 35 </ul> 36 </div> 37 </li> 38 <li><a href="#" id="submenu_a_2_2">二级菜单二</a></li> 39 <li><a href="#" id="submenu_a_2_3">二级菜单三</a></li> 40 <li><a href="#" id="submenu_a_2_4">二级菜单四</a></li> 41 <li><a href="#" id="submenu_a_2_5">二级菜单五</a></li> 42 </ul> 43 </div> 44 </li> 45 <li> 46 <a href="#" id="treemenu_a_3">一级菜单三</a> 47 <div class="submenu" id="submenu_3"> 48 <ul> 49 <li><a href="#" id="submenu_a_3_1">二级菜单一</a></li> 50 <li><a href="#" id="submenu_a_3_2">二级菜单二</a></li> 51 <li><a href="#" id="submenu_a_3_3">二级菜单三</a></li> 52 <li><a href="#" id="submenu_a_3_4">二级菜单四</a></li> 53 <li><a href="#" id="submenu_a_3_5">二级菜单五</a></li> 54 </ul> 55 </div> 56 </li> 57 </ul> 58 </div>
读取cookie工具类:
1 //cookie工具类 2 var cookieTool = { 3 //读取cookie 4 getCookie: function(c_name) { 5 if (document.cookie.length > 0) { 6 c_start = document.cookie.indexOf(c_name + "="); 7 if (c_start != -1) { 8 c_start = c_start + c_name.length + 1; 9 c_end = document.cookie.indexOf(";", c_start); 10 if (c_end == -1) { 11 c_end = document.cookie.length; 12 } 13 return unescape(document.cookie.substring(c_start, c_end)); 14 } 15 } 16 return ""; 17 }, 18 //设置cookie 19 setCookie: function(c_name, value, expiredays) { 20 var exdate = new Date(); 21 exdate.setDate(exdate.getDate() + expiredays); //设置日期 22 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); 23 }, 24 //删除cookie 25 delCookie: function(c_name) { 26 var exdate = new Date(); 27 exdate.setDate(exdate.getDate() - 1); //昨天日期 28 document.cookie = c_name + "=;expires=" + exdate.toGMTString(); 29 } 30 };
菜单事件绑定:
1 //菜单事件绑定 2 $(‘.treemenu a‘).bind(‘click‘, function() { 3 var $this = $(this); 4 var id = $this.attr(‘id‘); 5 var $submenu = $this.next(‘.submenu‘); 6 if ($submenu.length > 0) { //是否有子菜单 7 var flag = $(this).next(‘.submenu:hidden‘).length > 0 ? true : false; 8 if (flag) { 9 $submenu.show(); 10 } else { 11 $submenu.hide(); 12 } 13 var display = flag ? ‘block‘ : ‘none‘; 14 cookieTool.setCookie(id, display, 10); 15 } else { 16 cookieTool.setCookie(id, id, 10); 17 var curId = cookieTool.getCookie(id); 18 $(‘.treemenu‘).find(‘.on‘).removeClass(‘on‘).addClass(‘off‘); 19 $(‘#‘ + curId).addClass(‘on‘); 20 $(‘.treemenu a[class="off"]‘).each(function() { 21 cookieTool.delCookie($(this).attr(‘id‘)); //删除其他已选择选项cookie 22 }); 23 } 24 });
页面加载时重新设置菜单
1 //页面加载读取cookies 2 $(‘.treemenu a‘).each(function() { 3 showMenu($(this).attr(‘id‘)); 4 }); 5 6 7 //读取cookie显示菜单 8 function showMenu(id) { 9 var $this = $(‘#‘ + id); 10 var cookie = cookieTool.getCookie(id); 11 if (cookie) { 12 if ($this.next(‘.submenu‘).length > 0) { 13 $this.next(‘.submenu‘).css(‘display‘, cookie); 14 } else { 15 $(‘#‘ + cookie).addClass(‘on‘); 16 } 17 } 18 }
完整DEMO:
【JavaScript】刷新不变化树形菜单(多级菜单).zip
注意:chrome本地控制台无法读取cookie,需要在firefox/IE或者服务器环境下测试
时间: 2024-10-12 04:54:13