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