How can I recursively create a UL/LI's from JSON data - multiple layers deep

前端 未结 7 803
暖寄归人
暖寄归人 2020-12-30 12:32

I am trying to use use the following JSON data to create the following similar structure in a recursive inner function with not much luck, really need some help and so if an

7条回答
  •  盖世英雄少女心
    2020-12-30 13:03

    Make two functions makeUL and makeLI. makeUL calls makeLI on each element, and makeLI calls makeUL if there's sub elements:

    function makeUL(lst) {
        ...
        $(lst).each(function() { html.push(makeLI(this)) });
        ...
        return html.join("\n");
    }
    
    function makeLI(elem) {
        ...
        if (elem.sub)
            html.push('
    ' + makeUL(elem.sub) + '
    '); ... return html.join("\n"); }

    http://jsfiddle.net/BvDW3/

    Needs to be adapted to your needs, but you got the idea.

提交回复
热议问题