将Jquery对象更改为<ul> <li> </li> </ul>

时间:2013-07-21 18:59:16

标签: javascript jquery

我有以下jquery对象: -

        (function ($) {
            var menu = [{
                title: Main1",
                href: "/"
            }, {
                title: "Main2",
                href: "/main2",
                items: [{
                    title: "SubMain2_1",
                    href: "/subMain2_1"
                }, {
                    title: "SubMain2_2",
                    href: "/subMain2_2"
                }]
            }, {
                title: "Main3",
                href: "/main3",
                items: [{
                    title: "SubMain3_1",
                    href: "/subMain3_1"
                }, {
                    title: "SubMain3_2",
                    href: "/subMain3_2",
                    items: [{
                        title: "SubSubMain3_1",
                        href: "/SubSubMain3_2"
                    }, {
                        title: "SubSubMain3_2",
                        href: "/subSubMain3_2"
                    }]
                }, {
                    title: "SubMain3_3",
                    href: "/subMain3_3"
                }]
            }, {
                title: "Main4",
                href: "/main4"
            }];
        });

我希望将其改为

   <ul>
     <li class="Main"><a href="/">Main1</a></li>
     <li class="Main"><a href="/main2">Main2</a></li>
     <ul>
        <li class="Sub"><a href="/subMain2_1">SubMain2_1</a></li>
        <li class="Sub"><a href="/subMain2_2">SubMain2_2</a></li>
     </ul>
     <li class="Main"><a href="/main3">Main3</a></li>
     <ul>
        <li class="Sub"><a href="/subMain3_1">SubMain3_1</a></li>
        <li class="Sub"><a href="/subMain3_2">SubMain3_2</a></li>
        <ul>
           <li class="SubSub"><a href="/subSubMain3_1">SubSubMain3_1</a></li>
           <li class="SubSub"><a href="/subSubMain3_2">SubSubMain3_2</a></li>
        </ul>
        <li class="Sub"><a href="/subMain3_3">SubMain3_3</a></li>
     </ul>
     <li class="Main"><a href="/">Main4</a></li>
  </ul>

我怎样才能做到这一点?

感谢您的帮助和时间

3 个答案:

答案 0 :(得分:5)

你需要使用一个递归函数,每当有一个数组items时调用它自己:

$('body').append(fnMenu(menu));

function fnMenu(el){
    var ul = $('<ul>')
    $.each(el, function(){
        var li = $('<li>')
        li.append($('<a>', {text : this.title, href : this.href}));           
        ul.append(li)
        if(this.items) li.after(fnMenu(this.items)); 
    })
    return ul;
}

小提琴:http://jsfiddle.net/2hP9K/

答案 1 :(得分:1)

将此作为参考并继续。

    var  ul = $("<ul>");
        for (var i = 0, l = obj1.length; i < l; ++i) {
            ul.append("<li><a href='" + obj1[i].href + "'>" + obj1[i].title + "</a></li>");
        }
        $("#results").append(ul);    // add the list to the DOM
    });

FIDDLE

答案 2 :(得分:0)

尝试类似:

c = 0; 
function arrayer(rows,classe){
    c++;
    html = "";
    foreach(rows as row){
        html.='<ul class="'+classe+'">';
        if(row['items']!=""){ 
            html.='<li>';
            html.=arrayer(row['items'],'Sub');
            html.='</li>';
        }else{
            html.='<li class="'+row['title']+'" href="'+row['href']+'">'+classe+' '+c+'</li>';
        }
        html.='</ul>';
        return html;
    }
}

致电:

html_menu = arrayer(menu,'Main');
$('div.menu').append(html_menu);