js递归层级导航附源码下载
日期:2020-06-02
来源:程序思维浏览:1376次
咱们在做目录树无限级分类的时候,需要关联层级标题比如:第一层:女装,第二层:女装->潮流女装,第三层:女装->潮流女装->大码女装,第三层:女装->潮流女装->品牌女装,第四层:女装->潮流女装->大码女装->瘦身女装。这样的层级关系如何显示呢?
下面代码奉上:
<script>
var obj=[
{
"title":"A",
"children":[
{
"title":"A-1",
"children":[
{
"title":"A-1-1",
children:[]
},
{
"title":"A-1-2",
children:[]
}
]
},
{
"title":"A-2",
"children":[]
}
]
},
{
"title":"B",
"children":[
{
"title":"B-1",
"children":[]
},
{
"title":"B-2",
"children":[]
}
]
}
]
var pathTitle="";
function deep(obj){
for(var i=0;i<obj.length;i++){
//拼接标题
pathTitle+=obj[i].title+",";
console.log(pathTitle.substring(0,pathTitle.length-1));
if(obj[i].children && obj[i].children.length>0){
deep(obj[i].children)
}
//去除同级别上一个标题
var reg=eval("/"+obj[i].title+",/g");
pathTitle=pathTitle.replace(reg,"");
}
}
deep(obj);
</script>
百度网盘下载:链接:https://pan.baidu.com/s/1cqOGCU-EyG4aMyCscBD3Qg 密码:uyi2
下面代码奉上:
<script>
var obj=[
{
"title":"A",
"children":[
{
"title":"A-1",
"children":[
{
"title":"A-1-1",
children:[]
},
{
"title":"A-1-2",
children:[]
}
]
},
{
"title":"A-2",
"children":[]
}
]
},
{
"title":"B",
"children":[
{
"title":"B-1",
"children":[]
},
{
"title":"B-2",
"children":[]
}
]
}
]
var pathTitle="";
function deep(obj){
for(var i=0;i<obj.length;i++){
//拼接标题
pathTitle+=obj[i].title+",";
console.log(pathTitle.substring(0,pathTitle.length-1));
if(obj[i].children && obj[i].children.length>0){
deep(obj[i].children)
}
//去除同级别上一个标题
var reg=eval("/"+obj[i].title+",/g");
pathTitle=pathTitle.replace(reg,"");
}
}
deep(obj);
</script>
百度网盘下载:链接:https://pan.baidu.com/s/1cqOGCU-EyG4aMyCscBD3Qg 密码:uyi2
- 上一篇:史上最全video/audio监听事件
- 下一篇:js正则表达试里使用变量的几种方式
精品好课