


js uses cookies to refresh the tree menu without changing it_javascript skills
Save the state of the tree menu by setting a cookie, and re-read the cookie when the page loads to set the menu.
HTML structure of menu:
<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>
Read cookie tool class:
//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(); } };
Menu event binding:
//菜单事件绑定 $('.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 }); } });
Reset menu when page loads
//页面加载读取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'); } } }
Full DEMO:
[JavaScript] Refresh does not change the tree menu (multi-level menu).zip
Note: Chrome local console cannot read cookies and needs to be tested in firefox/IE or server environment

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When you browse the web on your iPhone, the loaded content is temporarily stored as long as the browser app remains open. However, the website updates content regularly, so refreshing the page is an effective way to clear out old data and see the latest published content. This way, you always have the latest information and experiences. If you want to refresh the page on iPhone, the following post will explain you all the methods. How to Refresh Web Pages on Safari [4 Methods] There are several methods to refresh the pages you are viewing on the Safari App on iPhone. Method 1: Use the Refresh Button The easiest way to refresh a page you have open on Safari is to use the Refresh option on your browser's tab bar. If Safa

Is the F5 key not working properly on your Windows 11/10 PC? The F5 key is typically used to refresh the desktop or explorer or reload a web page. However, some of our readers have reported that the F5 key is refreshing their computers and not working properly. How to enable F5 refresh in Windows 11? To refresh your Windows PC, just press the F5 key. On some laptops or desktops, you may need to press the Fn+F5 key combination to complete the refresh operation. Why doesn't F5 refresh work? If pressing the F5 key fails to refresh your computer or you are experiencing issues on Windows 11/10, it may be due to the function keys being locked. Other potential causes include the keyboard or F5 key

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.
