Native JS implements N-level menu code, js implements n

Source: Internet
Author: User

Native JS implements N-level menu code, js implements n

Requirement Analysis

After a simple analysis, to implement N-level menus, we should first start with the layout, that is, determine whether there are sub-menus

1. There is no next-level menu, which can be directly arranged

2. There are sub-menus, which can be divided into sub-menu emission locations. They are similar to the '>' symbol displayed in the parent menu, and the effect is as follows:

Figure: 1

Initial Implementation

1. Whether the implementation exists>

Note: Which of the following concerns are xxx ===yyy? Xxx: xxx uses a ternary expression to express its thoughts.

The HTML structure is as follows:

Figure: 2

• To achieve the effect shown in figure 1, we only need to judge the children. length = 2 in the li label? 'Span exist': 'span delete'

2. Position of the lower menu

The HTML structure is as follows:

• To meet this requirement, you also need to determine children. length = 2? 'The upper-level menu is relatively located, top is 0, left is the upper-level offsetWidth, and lower-level menu is absolutely positioned ': 'No processing is done'

The specific code is implemented

As follows:

The Code is as follows:

<! DOCTYPE html> 

The above is the native JS Code for implementing the N-level menu introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message. The editor will reply to you in time!

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.