我用递归方法写出一个树状结构,不过是嵌套一层。
比如说:
中国
    广东省
    湖南省
    安徽省
下面,我想要的结果是:
中国
    广东省
          广州市
          茂名市
          ……
我该怎么写???如果说用纯JS又该怎么去写???
我的e-mail地址:[email protected]

解决方案 »

  1.   

    你是想怎么做...存在数据库里读还是放到XML里读?或者有更好的办法?
      

  2.   

    楼主要JS的么?超级简单,比JAVA写得简单得多,呵呵。
    <html>
    <head> 
    <script type="text/javascript">function Tree(name, children) {
    this.name = name || "";
    this.children = children || []; this.appendChild = function(child) {
    this.children.push(child);
    } this.toString = function() {
    var result = "";
    result += "<div>|- " + this.name + "</div>";
    for (var i = 0; i < this.children.length; i++)
    result += "<div style='margin-left: 1em'>" + this.children[i] + "</div>";
    return result;
    }
    }var tree = new Tree("中国");
    tree.appendChild(new Tree("广东省", [new Tree("广州市"), new Tree("茂名市")]));
    </script>
    </head>   
    <body onload="document.getElementById('tree').innerHTML = tree">
    <div id="tree"></div>   
    </body>
    </html>