在jQuery Bootstrap for JSON Object中创建动态ul,li和菜单切换

时间:2016-10-18 15:00:51

标签: jquery json html5 twitter-bootstrap

我试图在JSON对象的jQuery引导程序中创建动态ul,li和菜单切换。

静态HTML代码正在运行,但在通过jQuery追加时,功能无效。

HTML:

<ul class="nav nav-pills nav-stacked toggle-menu" id="ListMenu">
    <li>
        <span class="submenu"><i class="fa fa-fw fa-male icon-push"></i><strong>Personality Insights</strong> </span>
        <ul class="list">
            <li class="open"><a href="javascript:void(0)" class="submenu"><i class="fa fa-angle-right"></i> Agreeableness</a><span class="badge">0.123 </span>
                <ul>
                    <li><a href="javascript:void(0)">Altruism</a><span class="badge">0.123 </span></li>
                </ul>
            </li>
        </ul>
    </li>
    <br>
    <li>
        <span><i class="fa fa-fw fa-tasks icon-push"></i><strong>Analysis Summary</strong> </span>
    </li><br>
    <li>
        <span><i class="fa fa-fw fa-home icon-push"></i><strong>Analysis Scores</strong> </span>
    </li>

jQuery的:

$(document).ready(function() {
 var j = 0;
 var k = 0;
 var l = 0;
 for (var i = 0; i < jsonData.response.tree.children.length; i++) {
     for (j; j < jsonData.response.tree.children[i].children.length; j++) {
         for (k; k < jsonData.response.tree.children[i].children[j].children.length; k++) {
             var name = jsonData.response.tree.children[i].children[j].children[k].name;
             var percent = jsonData.response.tree.children[i].children[j].children[k].percentage;
             $('.list').append(
                 '<li class="open"><a href="javascript:void(0)" class="submenu"><i class="fa fa-angle-right"></i> Agreeableness</a><span class="badge">0.123 </span><ul><li><a href="javascript:void(0)">Altruism</a><span class="badge">0.123 </span></li></ul></li>'
             );
             //$('.list').append('<li class="open "><a href="javascript:void(0)" class="submenu"><i class="fa fa-angle-right"></i>' + name +
             //  '</a><span class="badge">' + percent.toFixed(3) + ' </span><ul class="list1"></ul></li>');
             for (l; l < jsonData.response.tree.children[i].children[j].children[k].children.length; l++) {
                 //    $('.list1').append('<li><a href="javascript:void(0)" >caskj</a><span class="badge">0.123 </span></li>');

             }
         }
     }
    }
 });

Json数据

{
            "response": {
                "id": "*UNKNOWN*",
                "source": "*UNKNOWN*",
                "word_count": 724,
                "processed_lang": "en",
                "tree": {
                    "id": "r",
                    "name": "root",
                    "children": [{
                        "id": "personality",
                        "name": "Big 5",
                        "children": [{
                            "id": "Agreeableness_parent",
                            "name": "Agreeableness",
                            "category": "personality",
                            "percentage": 0.002759941616022221,
                            "children": [{
                                "id": "Openness",
                                "name": "Openness",
                                "category": "personality",
                                "percentage": 0.9418354354983149,
                                "sampling_error": 0.0602825656,
                                "children": [{
                                    "id": "Adventurousness",
                                    "name": "Adventurousness",
                                    "category": "personality",
                                    "percentage": 0.8611102572519946,
                                    "sampling_error": 0.0510810628
                                }, {
                                    "id": "Artistic interests",
                                    "name": "Artistic interests",
                                    "category": "personality",
                                    "percentage": 0.9011933811005671,
                                    "sampling_error": 0.1045761712
                                }, {
                                    "id": "Emotionality",
                                    "name": "Emotionality",
                                    "category": "personality",
                                    "percentage": 0.237370539866968,
                                    "sampling_error": 0.047803316
                                }, {
                                    "id": "Imagination",
                                    "name": "Imagination",
                                    "category": "personality",
                                    "percentage": 0.9761275915914904,
                                    "sampling_error": 0.0641724964
                                }, {
                                    "id": "Intellect",
                                    "name": "Intellect",
                                    "category": "personality",
                                    "percentage": 0.9983887028459361,
                                    "sampling_error": 0.0561232084
                                }, {
                                    "id": "Liberalism",
                                    "name": "Authority-challenging",
                                    "category": "personality",
                                    "percentage": 0.9966184165393535,
                                    "sampling_error": 0.0838070736
                                }]
                            }, {
                                "id": "Conscientiousness",
                                "name": "Conscientiousness",
                                "category": "personality",
                                "percentage": 0.12764016608507156,
                                "sampling_error": 0.0763236504,
                                "children": [{
                                    "id": "Achievement striving",
                                    "name": "Achievement striving",
                                    "category": "personality",
                                    "percentage": 0.6150687437706183,
                                    "sampling_error": 0.0987552864
                                }, {
                                    "id": "Cautiousness",
                                    "name": "Cautiousness",
                                    "category": "personality",
                                    "percentage": 0.23498615611263474,
                                    "sampling_error": 0.0918289024
                                }, {
                                    "id": "Dutifulness",
                                    "name": "Dutifulness",
                                    "category": "personality",
                                    "percentage": 0.007307952925156902,
                                    "sampling_error": 0.0600765668
                                }, {
                                    "id": "Orderliness",
                                    "name": "Orderliness",
                                    "category": "personality",
                                    "percentage": 0.07727331041517116,
                                    "sampling_error": 0.0703206668
                                }, {
                                    "id": "Self-discipline",
                                    "name": "Self-discipline",
                                    "category": "personality",
                                    "percentage": 0.05083766933523587,
                                    "sampling_error": 0.046976215599999996
                                }, {
                                    "id": "Self-efficacy",
                                    "name": "Self-efficacy",
                                    "category": "personality",
                                    "percentage": 0.9602484914372679,
                                    "sampling_error": 0.0923189036
                                }]
                            }, {
                                "id": "Extraversion",
                                "name": "Extraversion",
                                "category": "personality",
                                "percentage": 0.10935533520252294,
                                "sampling_error": 0.056834979199999996,
                                "children": [{
                                    "id": "Activity level",
                                    "name": "Activity level",
                                    "category": "personality",
                                    "percentage": 0.907602532577146,
                                    "sampling_error": 0.07797444320000001
                                }, {
                                    "id": "Assertiveness",
                                    "name": "Assertiveness",
                                    "category": "personality",
                                    "percentage": 0.9439147009874336,
                                    "sampling_error": 0.0832520224
                                }, {
                                    "id": "Cheerfulness",
                                    "name": "Cheerfulness",
                                    "category": "personality",
                                    "percentage": 0.004374312907113209,
                                    "sampling_error": 0.1047900296
                                }, {
                                    "id": "Excitement-seeking",
                                    "name": "Excitement-seeking",
                                    "category": "personality",
                                    "percentage": 0.8807806424077236,
                                    "sampling_error": 0.08077107
                                }, {
                                    "id": "Friendliness",
                                    "name": "Outgoing",
                                    "category": "personality",
                                    "percentage": 0.09017284779860157,
                                    "sampling_error": 0.0753163292
                                }, {
                                    "id": "Gregariousness",
                                    "name": "Gregariousness",
                                    "category": "personality",
                                    "percentage": 0.06440559422734304,
                                    "sampling_error": 0.0575984464
                                }]
                            }, {
                                "id": "Agreeableness",
                                "name": "Agreeableness",
                                "category": "personality",
                                "percentage": 0.002759941616022221,
                                "sampling_error": 0.0967609136,
                                "children": [{
                                    "id": "Altruism",
                                    "name": "Altruism",
                                    "category": "personality",
                                    "percentage": 0.2053855410551128,
                                    "sampling_error": 0.07061914720000001
                                }, {
                                    "id": "Cooperation",
                                    "name": "Cooperation",
                                    "category": "personality",
                                    "percentage": 0.10118376748829011,
                                    "sampling_error": 0.079838622
                                }, {
                                    "id": "Modesty",
                                    "name": "Modesty",
                                    "category": "personality",
                                    "percentage": 0.0002965573086819995,
                                    "sampling_error": 0.0558996024
                                }, {
                                    "id": "Morality",
                                    "name": "Uncompromising",
                                    "category": "personality",
                                    "percentage": 0.021057495604945742,
                                    "sampling_error": 0.06294283440000001
                                }, {
                                    "id": "Sympathy",
                                    "name": "Sympathy",
                                    "category": "personality",
                                    "percentage": 0.8128869859013528,
                                    "sampling_error": 0.0974131872
                                }, {
                                    "id": "Trust",
                                    "name": "Trust",
                                    "category": "personality",
                                    "percentage": 0.7796733414716182,
                                    "sampling_error": 0.0558053432
                                }]
                            }, {
                                "id": "Neuroticism",
                                "name": "Emotional range",
                                "category": "personality",
                                "percentage": 0.22555663220828476,
                                "sampling_error": 0.0909861776,
                                "children": [{
                                    "id": "Anger",
                                    "name": "Fiery",
                                    "category": "personality",
                                    "percentage": 0.964660722631485,
                                    "sampling_error": 0.0941762728
                                }, {
                                    "id": "Anxiety",
                                    "name": "Prone to worry",
                                    "category": "personality",
                                    "percentage": 0.8592923224573874,
                                    "sampling_error": 0.0551239012
                                }, {
                                    "id": "Depression",
                                    "name": "Melancholy",
                                    "category": "personality",
                                    "percentage": 0.8794771791692133,
                                    "sampling_error": 0.0587376276
                                }, {
                                    "id": "Immoderation",
                                    "name": "Immoderation",
                                    "category": "personality",
                                    "percentage": 0.1519853953350559,
                                    "sampling_error": 0.0526960524
                                }, {
                                    "id": "Self-consciousness",
                                    "name": "Self-consciousness",
                                    "category": "personality",
                                    "percentage": 0.9404370402836548,
                                    "sampling_error": 0.056498026
                                }, {
                                    "id": "Vulnerability",
                                    "name": "Susceptible to stress",
                                    "category": "personality",
                                    "percentage": 0.7972274596698029,
                                    "sampling_error": 0.0851245912
                                }]
                            }]
                        }]
                    }, {
                        "id": "needs",
                        "name": "Needs",
                        "children": [{
                            "id": "Curiosity_parent",
                            "name": "Curiosity",
                            "category": "needs",
                            "percentage": 0.9930774356093289,
                            "children": [{
                                "id": "Challenge",
                                "name": "Challenge",
                                "category": "needs",
                                "percentage": 0.8715044759547338,
                                "sampling_error": 0.083362816
                            }, {
                                "id": "Closeness",
                                "name": "Closeness",
                                "category": "needs",
                                "percentage": 0.011259953243068,
                                "sampling_error": 0.08234351999999999
                            }, {
                                "id": "Curiosity",
                                "name": "Curiosity",
                                "category": "needs",
                                "percentage": 0.9930774356093289,
                                "sampling_error": 0.119051472
                            }, {
                                "id": "Excitement",
                                "name": "Excitement",
                                "category": "needs",
                                "percentage": 0.24165267892652603,
                                "sampling_error": 0.1082397016
                            }, {
                                "id": "Harmony",
                                "name": "Harmony",
                                "category": "needs",
                                "percentage": 0.33223610643470525,
                                "sampling_error": 0.1083544888
                            }, {
                                "id": "Ideal",
                                "name": "Ideal",
                                "category": "needs",
                                "percentage": 0.7595619442096506,
                                "sampling_error": 0.0978071588
                            }, {
                                "id": "Liberty",
                                "name": "Liberty",
                                "category": "needs",
                                "percentage": 0.43056389906900344,
                                "sampling_error": 0.1443958456
                            }, {
                                "id": "Love",
                                "name": "Love",
                                "category": "needs",
                                "percentage": 0.33224854339347965,
                                "sampling_error": 0.0994201996
                            }, {
                                "id": "Practicality",
                                "name": "Practicality",
                                "category": "needs",
                                "percentage": 0.6862753127939327,
                                "sampling_error": 0.0861111356
                            }, {
                                "id": "Self-expression",
                                "name": "Self-expression",
                                "category": "needs",
                                "percentage": 0.2929367298685799,
                                "sampling_error": 0.08067559319999999
                            }, {
                                "id": "Stability",
                                "name": "Stability",
                                "category": "needs",
                                "percentage": 0.07385915424678774,
                                "sampling_error": 0.1052503724
                            }, {
                                "id": "Structure",
                                "name": "Structure",
                                "category": "needs",
                                "percentage": 0.5063534682278653,
                                "sampling_error": 0.0789028232
                            }]
                        }]
                    }, {
                        "id": "values",
                        "name": "Values",
                        "children": [{
                            "id": "Conservation_parent",
                            "name": "Conservation",
                            "category": "values",
                            "percentage": 0.008219832862913501,
                            "children": [{
                                "id": "Conservation",
                                "name": "Conservation",
                                "category": "values",
                                "percentage": 0.008219832862913501,
                                "sampling_error": 0.06733257919999999
                            }, {
                                "id": "Openness to change",
                                "name": "Openness to change",
                                "category": "values",
                                "percentage": 0.6420673522167587,
                                "sampling_error": 0.06416571239999999
                            }, {
                                "id": "Hedonism",
                                "name": "Hedonism",
                                "category": "values",
                                "percentage": 0.15679189194806414,
                                "sampling_error": 0.13657882159999998
                            }, {
                                "id": "Self-enhancement",
                                "name": "Self-enhancement",
                                "category": "values",
                                "percentage": 0.8934940816036145,
                                "sampling_error": 0.1026962488
                            }, {
                                "id": "Self-transcendence",
                                "name": "Self-transcendence",
                                "category": "values",
                                "percentage": 0.3262410113827664,
                                "sampling_error": 0.0802360844
                            }]
                        }]
                    }]
                },
                "warnings": []
            }
        };

0 个答案:

没有答案