js无限极分类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js无限极分类相关的知识,希望对你有一定的参考价值。
<script> var arr = [ {id:1,city:"水果",parent_id:0}, {id:2,city:"蔬菜",parent_id:0}, {id:3,city:"苹果",parent_id:1}, {id:4,city:"香菜",parent_id:2}, ]; var treeArr = []; function tree( arr , pid , level){ for (var i = 0; i < arr.length; i++) { if( arr[i].parent_id == pid ){ arr[i].level = level; treeArr.push(arr[i]); tree( arr,arr[i].id,level+1 ); } } } tree( arr , 0 , 0); for (var i = 0; i < treeArr.length; i++) { var tmpStr = ""; for (var j = 0; j < treeArr[i].level; j++) { tmpStr += "--"; } document.write( tmpStr + treeArr[i].city + "<br/>"); } </script>
以上是关于js无限极分类的主要内容,如果未能解决你的问题,请参考以下文章