使用 json 和 jquery 帮助实现多级菜单

发布于 2024-11-07 05:35:40 字数 2672 浏览 0 评论 0原文

我正在尝试做一个在“>”时滑动的多级菜单被点击。第一个问题是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2024-11-14 05:35:40

解决了。

使用 jQuery:

build_menu(json, 0);
    $('.back').hide();
    $('ul').not('.parent').hide();
});

function build_menu(json, parent, parentID) {
    var menu, li;
    var item = $('<ul class="menu ' + (parent == '0' ? 'parent' : '') + '" id="category' + parent + '"></ul>');
    var counter = 0;
    if (parent != '0') {
        li = $('<li><a class="back" href="#">Back</a></li>');
        li.click(function() {
            $('.back').hide();
            $("#category" + parentID).show();
            $("#category" + parent).hide();
            $('.back', $("#category" + parentID)).show();
            return false;
        })

        li.appendTo(item);
    }
    $.each(json, function(i, category) {
        if (category.parent == parent) {
            var more = $('<a class="more" href="#">></a>');
            more.click(function(e) {
                e.preventDefault();
                $('.back', $("#category" + category.id)).show();
                $("#category" + category.id).show();
                $("#category" + parent).hide();
                console.log("#category" + category.id, $("#category" + category.id));
                if ($("#category" + category.id).length <= 0) { //NO CHILDREN
                    $('.back').hide();
                    $("#category" + parent).show();
                    $('.back', $("#category" + parent)).show();
                }
                return false;
            })
            li = $('<li>' + category.name + '</li>');
            more.appendTo(li);
            li.appendTo(item);
            build_menu(json, category.id, parent);
            counter++;
        }
    });

    if (counter > 0) {
        menu = item;
        $('#menu').prepend(menu);
    }
}

小提琴: http://jsfiddle.net/maniator/CxBrW/25/< br>
摆弄幻灯片动画: http://jsfiddle.net/maniator/CxBrW/26/

<强>更新:

如果没有孩子,这里是没有胡萝卜的小提琴:http://jsfiddle。网/manianator/CxBrW/36/

Solved.

Use jQuery:

build_menu(json, 0);
    $('.back').hide();
    $('ul').not('.parent').hide();
});

function build_menu(json, parent, parentID) {
    var menu, li;
    var item = $('<ul class="menu ' + (parent == '0' ? 'parent' : '') + '" id="category' + parent + '"></ul>');
    var counter = 0;
    if (parent != '0') {
        li = $('<li><a class="back" href="#">Back</a></li>');
        li.click(function() {
            $('.back').hide();
            $("#category" + parentID).show();
            $("#category" + parent).hide();
            $('.back', $("#category" + parentID)).show();
            return false;
        })

        li.appendTo(item);
    }
    $.each(json, function(i, category) {
        if (category.parent == parent) {
            var more = $('<a class="more" href="#">></a>');
            more.click(function(e) {
                e.preventDefault();
                $('.back', $("#category" + category.id)).show();
                $("#category" + category.id).show();
                $("#category" + parent).hide();
                console.log("#category" + category.id, $("#category" + category.id));
                if ($("#category" + category.id).length <= 0) { //NO CHILDREN
                    $('.back').hide();
                    $("#category" + parent).show();
                    $('.back', $("#category" + parent)).show();
                }
                return false;
            })
            li = $('<li>' + category.name + '</li>');
            more.appendTo(li);
            li.appendTo(item);
            build_menu(json, category.id, parent);
            counter++;
        }
    });

    if (counter > 0) {
        menu = item;
        $('#menu').prepend(menu);
    }
}

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文