话不多说,直接上代码
function treeMenu(a){ this.tree=a||[]; this.groups={}; }; treeMenu.prototype={ init:function(pid){ this.group(); return this.getDom(this.groups[pid]); }, group:function(){ for(var i=0;i<this.tree.length;i++){ if(this.groups[this.tree[i].pId]){ this.groups[this.tree[i].pId].push(this.tree[i]); }else{ this.groups[this.tree[i].pId]=[]; this.groups[this.tree[i].pId].push(this.tree[i]); } } return this.groups; }, getDom:function(a){ if(!a){return ''} var html='\n<ul>\n'; for(var i=0;i<a.length;i++){ html+= '你的页面片段 比如<li></li>'; html+=this.getDom(this.groups[a[i].id]); html+='</li>\n'; }; html+='</ul>\n'; return html; } }; var html=new treeMenu(res.data).init(0), alert(html);