日期:2014-05-17  浏览次数:20681 次

JS 递归 循环未完成就被RETURN出来的问题.
在使用JS做递归的树结构时,函数中的FOR被自己给return出来了,导致循环没有完成


var json={
"type":"class",
    "text": "root",
    "children": [
        {
"type":"class",
            "text": "children1",
            "children": [
                {
"type":"class",
                    "text": "children1_1",
                    "children": [
                        {
"type":"value",
                            "text": "children1_1_value1"
                        },
                        {
"type":"value",
                            "text": "children1_1_value2"
                        }
                    ]
                }
            ]
        },
        {
"type":"class",
            "text": "children2",
            "children": [
                {
"type":"value",
                    "text": "children2_value1"
                },
                {
"type":"value",
                    "text": "children2_value2"
                }
            ]
        }
    ]
};

var xml="<root><class value='root'>";
xml += toxml(json['children']);
xml += "</class></root>";

function toxml(json){
var xml_str="";
for(i=0;i<json.length;i++){
if(json[i]["type"]=="class"){