我正在尝试使用 twitter bootstrap 构建动态菜单。这个想法是加载一个包含菜单项的 json 文件:
// test.json
{
"children": [
{
"text": "Item1",
"children": [
{"text": "Item11", "link": "/link11"},
{"text": "Item12", "link":"/link12"},
{"text": "Item13", "children": [
{"text": "Item131", "link":"/link131"},
{"text": "Item132", "link":"/link132"}
]},
{"text": "Item14", "link":"/link13"}
]},
{
"text": "Item2",
"children": [
{"text": "Item21", "link":"/link21"},
{"text": "Item22", "link":"/link22"}
]},
{
"text": "Item3"
}
]
}
这可以在 Twitter 引导程序中静态呈现:
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="navbar">
<div class="navbar-inner" id ="navbar-inner-main">
<div class="container">
<div class="nav-collapse collapse navbar-responsive-collapse">
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Item1</a>
<ul class="dropdown-menu">
<li>
<a href="/link11">Item11</a>
</li>
<li>
<a href="/link12">Item12</a>
</li>
<li class="dropdown-submenu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Item13</a>
<ul class="dropdown-menu">
<li>
<a href="/link131">Item131</a>
</li>
<li>
<a href="/link132">Item132</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Item2</a>
<ul class="dropdown-menu">
<li>
<a href="/link21">Item21</a>
</li>
<li>
<a href="/link22">Item22</a>
</li>
</ul>
</li>
<li>
<a href="#">Item3</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
我想在 index.html 中添加这样的函数:
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('ajax/test.json', function(data) {
// get json, parse it and append to .nav
});
});
</script>
我认为将 json 解析为无序列表的最佳方法是使用模板。我尝试了纯 和小胡子,但没有成功。问题是由于下拉子菜单类使递归方法不一致。
你有什么建议吗?
提前致谢