Simple implementation of js click expand Level 2 menu function, js click expand menu

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.