我的任务是从 json 文件中执行水平菜单。通过这样做,我没有得到子子项。我的 json 文件是
var data = [{
"menu":[
{
"MenuId":1,
"MenuName":"Home",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":1,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":2,
"MenuName":"New Transaction",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":2,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":3,
"MenuName":"Portfolio",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":3,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":4,
"MenuName":"Analytics",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":4,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":5,
"MenuName":"Instructions",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":5,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":6,
"MenuName":"Data Upload",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":6,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":7,
"MenuName":"Administration",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":null,
"SortOrder":7,
"ModuleId":null,
"Menus":[
{
"MenuId":8,
"MenuName":"Masters",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":7,
"SortOrder":1,
"ModuleId":null,
"Menus":[
{
"MenuId":10,
"MenuName":"Currency",
"MenuLink":"/Currencies/Index",
"Action":"Index",
"Controller":"Currencies",
"ParentID":8,
"SortOrder":1,
"ModuleId":null,
"Menus":{
}
},
{
"MenuId":11,
"MenuName":"Country",
"MenuLink":"/Countries/Index",
"Action":"Index",
"Controller":"Countries",
"ParentID":8,
"SortOrder":2,
"ModuleId":null,
"Menus":{
}
}
]
},
{
"MenuId":9,
"MenuName":"User Management",
"MenuLink":null,
"Action":null,
"Controller":null,
"ParentID":7,
"SortOrder":2,
"ModuleId":null,
"Menus":{
}
}
]
}
]
}
];
$(function() {
for(var i = 0, j = data[0].menu.length; i<j; i++) {
var root_menu = data[0].menu[i];
if(root_menu.hasOwnProperty("MenuId")) {
$("#menu").append('<li><a href="#" class="parent-menu">' + root_menu.MenuName + '</a></li>');
if(root_menu.hasOwnProperty("Menus") && root_menu.Menus.length > 0) {
$("#menu").append("<li><ul class='child-list' id='menu_" + root_menu.MenuId + "'>");
for(var n = 0, m = root_menu.Menus.length; n<m; n++) {
var sub_menu = root_menu.Menus[n];
if(sub_menu.hasOwnProperty("MenuId")) {
$("#menu_" + root_menu.MenuId).append("<li class='nav-child'>" + sub_menu.MenuName + "</li>");
}
}
$("#menu").append("</li></ul>");
}
}
}
});
如何使用此 json 文件获取子子项?您可以从http://jsfiddle.net/JcU4G/9/看到我的代码 在此管理菜单中有两个子菜单,该子菜单有另一个子菜单。在小提琴中,您可以看到子菜单位于主页下。但实际上我想要管理菜单下的子菜单,这些子菜单主包含另一个大子菜单。