首先需要引入jQuery哈!

1. 要求用下面的格式制作目录, 结构如下:
<ul>
<li>xxxx</li>
<li>xxxx</li>
<ul>
<li>xxxx</li>
<li>xxxx</li>
<ul>
<li>xxxx</li>
<li>xxxx</li>
</ul>
</ul>
</ul>
2. 要求带锚点的, 要使用父节点ID+锚点做为链接

1. 后台返回的数据:

const newDirList = [
{
"catalogId": 1,
"catalogFid": 0,
"catalogName": "目录",
"child": [],
"anchor": ""
},
{
"catalogId": 2,
"catalogFid": 0,
"catalogName": "版权信息",
"child": [],
"anchor": ""
},
{
"catalogId": 3,
"catalogFid": 0,
"catalogName": "第1章",
"child": [
{
"catalogId": 301,
"catalogFid": 3,
"catalogName": "第1章第1节",
"child": [
{"catalogId": 30101, "catalogFid": 301, "catalogName": "第1章第1节第1段", "child": [], "anchor": ""},
{"catalogId": 30102, "catalogFid": 301, "catalogName": "第1章第1节第2段", "child": [], "anchor": ""},
{"catalogId": 30103, "catalogFid": 301, "catalogName": "第1章第1节第3段", "child": [], "anchor": ""},
{"catalogId": 30104, "catalogFid": 301, "catalogName": "第1章第1节第4段", "child": [], "anchor": ""}
],
"anchor": ""
},
{
"catalogId": 302,
"catalogFid": 3,
"catalogName": "第1章第2节",
"child": [
{"catalogId": 30201, "catalogFid": 302, "catalogName": "第1章第2节第1段", "child": [], "anchor": "sec01"},
{"catalogId": 30202, "catalogFid": 302, "catalogName": "第1章第2节第2段", "child": [], "anchor": "sec02"},
{"catalogId": 30203, "catalogFid": 302, "catalogName": "第1章第2节第3段", "child": [], "anchor": "sec03"},
{"catalogId": 30204, "catalogFid": 302, "catalogName": "第1章第2节第4段", "child": [], "anchor": "sec04"}
],
"anchor": ""
},
{
"catalogId": 303,
"catalogFid": 3,
"catalogName": "第1章第3节",
"child": [],
"anchor": ""
},
{
"catalogId": 304,
"catalogFid": 3,
"catalogName": "第1章第4节",
"child": [],
"anchor": ""
},
{
"catalogId": 305,
"catalogFid": 3,
"catalogName": "第1章第5节",
"child": [],
"anchor": ""
}
],
"anchor": ""
},
{
"catalogId": 4,
"catalogFid": 0,
"catalogName": "第2章",
"child": [],
"anchor": ""
},
{
"catalogId": 6,
"catalogFid": 0,
"catalogName": "后记",
"child": [],
"anchor": ""
}
]

2. javaScrip:

 function genLi(name, catalogId, anchor, catalogFid) {
return `
<li>
<a href="${anchor?catalogFid:catalogId}.xhtml${anchor?'#'+anchor:''}">${name}(${anchor?catalogFid:catalogId}.xhtml${anchor?'#'+anchor:''})</a>
</li>
`.trim();
}
function loopArray (arr) {
let $ul = $('<ul>');
arr.forEach(function(v){
let $li = $(genLi(v.catalogName, v.catalogId, v.anchor, v.catalogFid));
$ul.append($li);
if (v.child && v.child.length) {
$ul.append(loopArray(v.child));
}
});
return $ul.get(0).outerHTML;
}
loopArray(newDirList);

3. 生成出的HTML代码:

<ul>
<li>
<a href="1.xhtml">目录(1.xhtml)</a>
</li>
<li>
<a href="2.xhtml">版权信息(2.xhtml)</a>
</li>
<li>
<a href="3.xhtml">第1章(3.xhtml)</a>
</li>
<ul>
<li>
<a href="301.xhtml">第1章第1节(301.xhtml)</a>
</li>
<ul>
<li>
<a href="30101.xhtml">第1章第1节第1段(30101.xhtml)</a>
</li>
<li>
<a href="30102.xhtml">第1章第1节第2段(30102.xhtml)</a>
</li>
<li>
<a href="30103.xhtml">第1章第1节第3段(30103.xhtml)</a>
</li>
<li>
<a href="30104.xhtml">第1章第1节第4段(30104.xhtml)</a>
</li>
</ul>
<li>
<a href="302.xhtml">第1章第2节(302.xhtml)</a>
</li>
<ul>
<li>
<a href="302.xhtml#sec01">第1章第2节第1段(302.xhtml#sec01)</a>
</li>
<li>
<a href="302.xhtml#sec02">第1章第2节第2段(302.xhtml#sec02)</a>
</li>
<ul>
<li>
<a href="3020201.xhtml">第1章第2节第2段第1行(3020201.xhtml)</a>
</li>
<li>
<a href="3020202.xhtml">第1章第2节第2段第2行(3020202.xhtml)</a>
</li>
<li>
<a href="3020203.xhtml">第1章第2节第2段第3行(3020203.xhtml)</a>
</li>
<li>
<a href="3020204.xhtml">第1章第2节第2段第4行(3020204.xhtml)</a>
</li>
</ul>
<li>
<a href="302.xhtml#sec03">第1章第2节第3段(302.xhtml#sec03)</a>
</li>
<li>
<a href="302.xhtml#sec04">第1章第2节第4段(302.xhtml#sec04)</a>
</li>
</ul>
<li>
<a href="303.xhtml">第1章第3节(303.xhtml)</a>
</li>
<li>
<a href="304.xhtml">第1章第4节(304.xhtml)</a>
</li>
<li>
<a href="305.xhtml">第1章第5节(305.xhtml)</a>
</li>
</ul>
<li>
<a href="4.xhtml">第2章(4.xhtml)</a>
</li>
<li>
<a href="6.xhtml">后记(6.xhtml)</a>
</li>
</ul>

根据返回数据, 迭代数组, 构造HTML结构的更多相关文章

  1. matlab学习笔记12_2创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段

    一起来学matlab-matlab学习笔记12 12_2 结构体 创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段 觉得有用的话 ...

  2. C语言实现使用动态数组来构造栈结构

    我在面前一篇博客<C语言实现使用静态数组来构造栈结构>中使用了静态数组来模拟栈的操作.静态数组的大小是在代码中写死的.是存储在用户栈上面的,使用起来不灵活.在这篇博客中我会使用动态数组来构 ...

  3. 013.CI4框架CodeIgniter数据库操作之:查询数据库,并让数据以数组的方式返回查询结果

    01. 我们在CI4框架中的Model文件夹新建一个User_model.php的文件,使用的是getResultArray,表示并让数据以数组的方式返回查询结果,代码如下: <?php nam ...

  4. carry-检查数据接口返回数据合法性

    问题背景: 在测试&部署监控过程中,我们常常会遇到外部接口返回数据不靠谱的时候.最常见的场合是从某个http获取如json和xml等结构化的结果,进行解析并处理,在这时候出现以下这几种常见类型 ...

  5. ListFiles():返回Files类型数组,可以用getName()来访问到文件名。

    List():显示文件的名(相对路径) ListFiles():返回Files类型数组,可以用getName()来访问到文件名. 使用isDirectory()和isFile()来判断究竟是文件还是目 ...

  6. NumPy来自现有数据的数组

    NumPy - 来自现有数据的数组 这一章中,我们会讨论如何从现有数据创建数组. numpy.asarray 此函数类似于numpy.array,除了它有较少的参数. 这个例程对于将 Python 序 ...

  7. JavaScript 构造树形结构的一种高效算法

    引言 我们经常会碰到树形数据结构,比如组织层级.省市县或者动植物分类等等数据.下面是一个树形结构的例子: 在实际应用中,比较常见的做法是将这些信息存储为下面的结构,特别是当存在1对多的父/子节点关系时 ...

  8. NumPy 基于已有数据创建数组

    原文:Python Numpy 教程 章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基 ...

  9. pandas:数据迭代、函数应用

    1.数据迭代 1.1 迭代行 (1)df.iterrows() for index, row in df[0:5].iterrows(): #需要两个变量承接数据 print(row) print(& ...

随机推荐

  1. consul ocelot

    consul配置完成后 新建.netcoreapi项目, nuget安装ocelot 添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置 Serv ...

  2. HTTP状态码分类及异常状态码处理

    1xx:表示临时响应100:(继续)请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分101:(切换协议)请求者已要求服务器切换协议,服务器已确认并准备切换 2xx:表 ...

  3. 11. Ingress及Ingress Controller(主nginx ingress controller)

    11. Ingress,Ingress Controller拥有七层代理调度能力 什么是Ingress: Ingress是授权入站连接到达集群服务的规则集合 Ingress是一个Kubernetes资 ...

  4. 深度学习之NLP获取词向量

    1.代码 def clean_text(text, remove_stopwords=False): """ 数据清洗 """ text = ...

  5. smarty section 循环不同的四个样式

    <div class="moban_spzs"> <{section name=goodslist loop=$strdata6}> <{if $sm ...

  6. switch语句 initialization of 'XXX' is skipped by 'case' label 原因及解决办法--块语句的作用

    出错代码段: switch (t) { case 0:  int a = 0;  break; default:  break; }编译时提示:“error C2361: initialization ...

  7. spring配置注解context:annotation-config和context:component-scan区别

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

  8. nexus私服库被误删如何恢复

    恢复步骤: 1,登录nexus服务器,找到nexus安装目录(默认/usr/local/下): 2,找到sonatype-work/nexus/trash 下找到你删除的库: 3,copy到指定的so ...

  9. Django组件-admin

    一. admin组件的使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INST ...

  10. ERROR 2002 (HY000): Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) "

    找不到mysql.sock这个文件 如果在你操作安装提示创建该文件,重启服务器还是提示这个错误可以试一下 mysql -uroot -h 127.0.0.1 -p 应该是可以直接进入 具体处理方法 重 ...