接口返回的数据格式可参照:1 {…}
name 城中区
count 6
id 1
sort 0
childrenList […]
0 {…}
name 城中街道
count 2
id 2
sort 0
childrenList []
1 {…}
2 {…}
3 {…}
4 {…}
5 {…}
以下是动态构造树状的方法:
function buildMenu(rdata) { var temp = ''; $(rdata).each(function() { if (this.childrenList.length != 0) { temp += '<li><div class="close_menu"><span></span><a title="' + this.name + '" id="' + this.id + '">' + this.name + '</a></div>'; temp += '<ul parentId="' + this.id + '" parentName="' + this.name + '" style="display:none;">'; temp += buildMenu(this.childrenList); temp += '</ul></li>'; } else { temp += '<li><a title="' + this.name + '" id="' + this.id + '">' + this.name + '</a></li>' } }); return temp; }