js以递归实现:
var city = [
{ name : "北京",
children : [
{ name : "海淀区",
children : [
{ name : "1环",
children : [
{ name : "10环"},
{ name : "11环"}
]
},
{ name : "2环"}
]
},
{ name : "朝阳区"}
]
},
{ name : "浙江",
children : [
{ name : "杭州"},
{ name : "宁波"}
]
},
{ name : "广东",
children : [
{ name : "1环"},
{ name : "2环"}
]
},
{ name : "深圳" }
];
function getChild( parents ){
var childUl = "";
if( parents){
for(var i = 0; i < parents.length; i++ ){
childUl += "<li>" + parents[i].name + "</li>"; //同级元素用li标签
if(parents[i].children){ //判断是否当前元素是否有子节点
var childNode = getChild( parents[i].children ); //返回子节点
//把子字节点