Simple implementation of js click expand Level 2 menu function, js click expand menu
Although jQuery is already very useful, there are still many restrictions in actual development projects. For example, the wonderful requirements of the project team cannot use any plug-ins. Of course, the plug-ins also take up resources, after all, 100 + KB is very large for small projects. I recently met the requirement to click to expand the level-2 menu. Of course, I can only write it using native JS. I have used an online case to add it and share it with me:
If the level-2 menu is hidden when the page is opened by default, You need to click to display the level-2 menu, and then click to hide the level-2 menu. Here there are two vertices for display and hiding: display = "block" and display = "none". The other is to make a judgment. if else judges whether the current is block or none.
<! DOCTYPE html>
Note that the label cannot be added to the li of the level-1 menu, otherwise it will not work.
If the second-level menu is displayed on your page by default, it is disabled only when you click it. Remove the style display = "none" of the style label. You also need to modify js.
<script type="text/javascript"> function f(str){ var sub_menu = document.getElementById(str); var dis_v = sub_menu.style.display; if(dis_v == "none") sub_menu.style.display = "block"; else sub_menu.style.display = "none"; } </script>
Take a closer look, or you will find that you need to click twice to see the desired effect.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.