0

我是 Jquery 和 web 应用程序开发的新手,我有一个 JSON 文件定义为下面的对象,我想读取它并在网页上将其元素显示为 HTML 元素对于下面的 JSON 文件,我希望输出如下:

            A-1
            AA-0
            AB-0
            AAA-0
            AAB-2
            ABA-0
            ABB-1 

稍后我计划在单击事件上显示当前组件下的组件及其状态

我写了下面的代码

            <!DOCTYPE html>
            <html>
            <head>

                <script src="jquery-1.9.1.js"></script>
            </head>
            <body>
              <div id="result">

            </div>
            <script>
            var obj = {
                        "component": "A",
                        "status": 0,
                        "children": [
                            {
                                "component": "AA",
                                "status": 0,
                                "children": [
                                    {
                                        "component": "AAA",
                                        "status": 0,
                                        "children": []
                                    },
                                    {
                                        "component": "AAB",
                                        "status": 2,
                                        "children": []
                                    }
                                ]
                            },
                            {
                                "component": "AB",
                                "status": 0,
                                "children": [
                                    {
                                        "component": "ABA",
                                        "status": 0,
                                        "children": []
                                    },
                                    {
                                        "component": "ABB",
                                        "status": 1,
                                        "children": []
                                    }
                                ]

                            }
                        ]
            };
            //Start function
            $(function() {
                var result = $('#result');
                alert("hello");
                procedure(obj);
            });

            //Function to display the list of children of a given object
            function display(dref){

                result.append('<div>' +  dref.component + ' - '+ dref.status +'</div>');    
                $.each(dref.children, function(i, v){
                result.append('<div>' +  v.component + ' - '+ v.status +'</div>');
            });
            };

            //Recursive function to repeatedly check for children of every component
            function procedure(pref){
                display(pref);
                if(pref.children not null)
                {
                    $.each(pref.children, function(i, v){
                    procedure(children)

                    }    
            }   
            };
            </script> 
            </body>
            </html>

有些地方有语法错误,谁能帮我得到所需的输出

4

2 回答 2

1

尝试

$(function(){
    var $result = $('#result');
    function process(obj, flag){
        if(!flag){
            $('<li/>').text(obj.component + '-'+obj.status).appendTo($result);
        }

        if(obj.children){
            $.each(obj.children, function(i, v){
                $('<li/>').text(this.component + '-'+this.status).appendTo($result);
            });
            $.each(obj.children, function(i, v){
                process(this, true);
            });
        }
    }

    process(obj);

});

演示:小提琴

于 2013-04-02T12:46:55.540 回答
1

您是否正在寻找这个:

var result_temp = "";
    function procedure(pref) {
        result_temp = result_temp + pref.component + "-" + pref.status+"<br/>";
        if (pref.children != null) {
            $.each(pref.children, function (i, v) {
                //alert(JSON.stringify(v));
                procedure(v);

            });
        }
    }

    $(document).ready(function () {
        procedure(obj); 
     $("#answer").html(result_temp);
    });

这里“#answer”是你的结果 div。

于 2013-04-02T12:51:38.577 回答