使用jQuery / JSON的导航菜单

时间:2013-01-28 14:36:28

标签: javascript jquery html css

如何仅使用jQuery / JSON构建以下导航菜单? enter image description here

我的JSON如下;

{
   "general":{
      "logo":"assets/images/logo.png",
      "name" : "Company Name"
   },
   "navigation":[
      {
         "title":"Home",
         "link":"index.html"
      },
      {
         "title":"Products",
         "link":"product.html",
         "subnav":[
            {
               "title":"Webapps",
               "link":"products/w.html"
            },
            {
               "title":"Mobile Apps",
               "link":"products/m.html"
            }
         ]
      }
   ]
}

我会避免除了jQuery以外的任何事情。

1 个答案:

答案 0 :(得分:1)

如果您的问题只是遍历json ..这可能会有所帮助。

for (i in jar.navigation) {
                alert(jar.navigation[i].title);
                var hassub = (jar.navigation[i].subnav != null);                
                if (hassub) {                                        
                    for (j in jar.navigation[i].subnav) {
                        alert(jar.navigation[i].subnav[j].title);
                    }}
            }