使用 json 和 jquery 帮助实现多级菜单
我正在尝试做一个在“>”时滑动的多级菜单被点击。第一个问题是 CSS 无法正常工作,< ul > 不会向左浮动。
我需要的是只显示一个 ul,如果“>”单击显示“子”菜单,滑动它或显示它,任何方法都有用。
准备测试的完整代码是这里
我正在尝试这样做:菜单
JavaScript
$(document).ready(function(){
var json = [{"id":"1","parent":"0","slug":"digitalart","name":"Digital Art"},{"id":"2","parent":"1","slug":"3d","name":"3-Dimensional Art"},{"id":"39","parent":"1","slug":"drawings","name":"Drawings"},{"id":"3","parent":"2","slug":"abstract","name":"Abstract"},{"id":"4","parent":"2","slug":"characters","name":"Characters"},{"id":"12","parent":"2","slug":"objects","name":"Objects"},{"id":"23","parent":"2","slug":"scenes","name":"Scenes"},{"id":"32","parent":"2","slug":"unsorted","name":"Unsorted"},{"id":"33","parent":"2","slug":"vehicles","name":"Vehicles"},{"id":"5","parent":"4","slug":"creatures","name":"Animals & Creatures"},{"id":"6","parent":"4","slug":"cartoon","name":"Cartoon"},{"id":"7","parent":"4","slug":"female","name":"Female"},{"id":"8","parent":"4","slug":"groups","name":"Groups"},{"id":"9","parent":"4","slug":"machines","name":"Machines & Robots"},{"id":"10","parent":"4","slug":"male","name":"Male"},{"id":"11","parent":"4","slug":"misc","name":"Miscellaneus"}];
build_menu(json, 0);
});
function build_menu(json, parent){
var menu;
var item = "";
var counter = 0;
if(parent != '0'){
item += '<li><a class="more" onClick="show(); return false;" href="#">Back</a></li>';
}
$.each(json, function(i, category) {
if(category.parent == parent){
var more = '<a class="more" onClick="show('+parent+'); return false;" href="#">></a>';
item = item + '<li>' + category.name + more + '</li>';
build_menu(json, category.id);
counter++;
}
});
if(counter > 0){
menu = '<ul class="menu" id="category' + parent + '">' + item + '</ul>';
$('#menu').prepend(menu);
}
}
function show(id){
$(".menu").hide();
$("#category"+id).show();
}
css
#menu{
width: 180px;
overflow: hidden;
}
#menu ul{
width: 180px;
float: left;
list-style: none;
}
#menu ul li{
border: solid 1px black;
margin-bottom: 5px;
}
#menu li .more{
//float: right;
text-decoration: none;
margin-right: 5px;
}
html
<div id="menu">
</div>
I'm trying to do a multilevel menu that slides when ">" is clicked. The first issue is that the CSS is not working properly, the < ul >'s aren't getting floated to the left.
What I'm needing is show only one ul, and if ">" is clicked show the "children" menu sliding it or showing it, any method is useful.
The complete code ready to test is here
I'm trying to do this: menu
JavaScript
$(document).ready(function(){
var json = [{"id":"1","parent":"0","slug":"digitalart","name":"Digital Art"},{"id":"2","parent":"1","slug":"3d","name":"3-Dimensional Art"},{"id":"39","parent":"1","slug":"drawings","name":"Drawings"},{"id":"3","parent":"2","slug":"abstract","name":"Abstract"},{"id":"4","parent":"2","slug":"characters","name":"Characters"},{"id":"12","parent":"2","slug":"objects","name":"Objects"},{"id":"23","parent":"2","slug":"scenes","name":"Scenes"},{"id":"32","parent":"2","slug":"unsorted","name":"Unsorted"},{"id":"33","parent":"2","slug":"vehicles","name":"Vehicles"},{"id":"5","parent":"4","slug":"creatures","name":"Animals & Creatures"},{"id":"6","parent":"4","slug":"cartoon","name":"Cartoon"},{"id":"7","parent":"4","slug":"female","name":"Female"},{"id":"8","parent":"4","slug":"groups","name":"Groups"},{"id":"9","parent":"4","slug":"machines","name":"Machines & Robots"},{"id":"10","parent":"4","slug":"male","name":"Male"},{"id":"11","parent":"4","slug":"misc","name":"Miscellaneus"}];
build_menu(json, 0);
});
function build_menu(json, parent){
var menu;
var item = "";
var counter = 0;
if(parent != '0'){
item += '<li><a class="more" onClick="show(); return false;" href="#">Back</a></li>';
}
$.each(json, function(i, category) {
if(category.parent == parent){
var more = '<a class="more" onClick="show('+parent+'); return false;" href="#">></a>';
item = item + '<li>' + category.name + more + '</li>';
build_menu(json, category.id);
counter++;
}
});
if(counter > 0){
menu = '<ul class="menu" id="category' + parent + '">' + item + '</ul>';
$('#menu').prepend(menu);
}
}
function show(id){
$(".menu").hide();
$("#category"+id).show();
}
css
#menu{
width: 180px;
overflow: hidden;
}
#menu ul{
width: 180px;
float: left;
list-style: none;
}
#menu ul li{
border: solid 1px black;
margin-bottom: 5px;
}
#menu li .more{
//float: right;
text-decoration: none;
margin-right: 5px;
}
html
<div id="menu">
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。
使用 jQuery:
小提琴: http://jsfiddle.net/maniator/CxBrW/25/< br>
摆弄幻灯片动画: http://jsfiddle.net/maniator/CxBrW/26/
<强>更新:
如果没有孩子,这里是没有胡萝卜的小提琴:http://jsfiddle。网/manianator/CxBrW/36/
Solved.
Use jQuery:
Fiddle: http://jsfiddle.net/maniator/CxBrW/25/
Fiddle with slide animations: http://jsfiddle.net/maniator/CxBrW/26/
UPDATE:
Here is fiddle with no carrots if no children: http://jsfiddle.net/maniator/CxBrW/36/