首先需要引入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. 把文档中的数据取出并插入到excel中

    from xlrd import open_workbookfrom xlutils.copy import copy jsonfile=r'C:\Users\Administrator\Deskto ...

  2. python同时执行两个函数

    使用两个线程同时执行两个函数, def fun1(): while True: time.sleep(2) print("fun1") def fun2(): while True ...

  3. [ML] Load and preview large scale data

    Ref: [Feature] Preprocessing tutorial 主要是 “无量纲化” 之前的部分. 加载数据 一.大数据源 http://archive.ics.uci.edu/ml/ht ...

  4. java使用nio(Paths,Files)遍历文件目录,转成java.io.File

    String directory = "C:\\Users\\Administrator\\AppData\\Local\\Temp\\8ad088a2-0bb3-41dc-89d9-2c5 ...

  5. mysql8修改密码问题

    查看初始密码: grep "temporary password" /var/log/mysqld.log 查看validate_password变量 SHOW VARIABLES ...

  6. java中,有关移位运算符的有关讨论

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  7. python之selenium元素定位方法

    前提: 大家好,今天我们来学习一下selenium,今天主要讲解selenium定位元素的方法,希望对大家有所帮助! 内容: 一,selenium定位元素 selenium提供了8种方法: 1.id ...

  8. 【FFMPEG】使用ffmpeg类库打开流媒体

    版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函 ...

  9. shell-常用命令,重定向和文件包含

    shell的知识点并不多,这里简单介绍一下常用的一些东西 常用命令 echo 显示普通字符串 echo "test" 显示转义字符 echo "\"test\& ...

  10. jQuery 相关插件

    jQuery 是一个快速的,简洁的 javaScript 库,使用户能更方便地处理 HTML documents.events.实现动画效果,并且方便地为网站提供 AJAX 交互. jQuery 还有 ...