JS dynamic load div Display main Menu and submenu +jquery get dynamic ID

Source: Internet
Author: User

Recently in the work of the main interface of project refactoring, it is necessary to dynamically display the main menu and sub-menu (link of each interface), to see that the old system is using ASP control, but the MVC framework is used in refactoring, and these menus are not dead, but are configured by other interfaces. Both the main menu and the submenu are database query displays. Therefore, we think of the dynamic splicing div to achieve the effect.


to achieve the



implementation steps:

The first step is to query the main menu name

The second step, query the specific interface name

The third step, the menu ID to achieve the main menu and sub-menu matching


Implementation ideas:
Each main menu is a Div, the submenu under the main menu is a whole div, each specific submenu is li.


Code implementation:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >$ (function () {$.post ('/main/queryallmenu ', {}, function (data) {$.post ('/main/queryallinterface ', {}, fun            Ction (DATA2) {//Add the Data $ ("#menu-container") in the Div first. empty (); Traverse the menu for adding a query to the DIV for (var n = 0; n < data.length; n++) {var menuid = Data[n]. menuid//Sub-menu div dynamic ID $ ("#menu-container"). Append ("<div class=menu-tit id=menuname Ind Ex= "+ n +" ><input type=hidden > "+ data[n". MenuName + "</div><div class=menu-list ><div class=top-line></div><ul class=nav-items id="                    + MenuID + "></ul></div>") for (var i = 0; i < data2.length; i++) { if (Data[n]. MenuID = = Data2[i]. MenuID) {$ ("#" + MenuID + ""). Append ("<li><a Href= "+ data2[i]. InterfacePath + "Target=content id=windowsname onclick=alerttext ('" + data2[i]. InterfaceName + "') >" + data2[i ".                    InterfaceName + "</a></li>")}}} index.init ();    }); });}); </span>

PS:

$ ("#" +id+ "") gets the dynamic ID, so the above $ ("#" + MenuID + "") This code is easy to understand, dynamically get the ID of the menu


post-run code stitching effect:



write in the back:

At this point the basic ability to achieve, the next step to achieve the sliding effect of the menu, I have also written their own Div, the original so simple, there are many features worthy of my study, the next blog Introduction menu sliding effect implementation.

JS dynamic load div Display main Menu and submenu +jquery get dynamic ID

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.