js 通过cookie实现刷新不变化树形菜单

时间:2022-12-09 15:42:18

通过设置cookie来保存树形菜单的状态,在页面加载时重新读取cookie来设置菜单。

菜单的HTML结构:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<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工具类:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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();
}
};

菜单事件绑定:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//菜单事件绑定
$('.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
});
}
});

页面加载时重新设置菜单

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//页面加载读取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或者服务器环境下测试