首先需要引入jQuery哈!

  1. 1. 要求用下面的格式制作目录, 结构如下:
  2. <ul>
  3. <li>xxxx</li>
  4. <li>xxxx</li>
  5. <ul>
  6. <li>xxxx</li>
  7. <li>xxxx</li>
  8. <ul>
  9. <li>xxxx</li>
  10. <li>xxxx</li>
  11. </ul>
  12. </ul>
  13. </ul>
  14. 2. 要求带锚点的, 要使用父节点ID+锚点做为链接

1. 后台返回的数据:

  1. const newDirList = [
  2. {
  3. "catalogId": 1,
  4. "catalogFid": 0,
  5. "catalogName": "目录",
  6. "child": [],
  7. "anchor": ""
  8. },
  9. {
  10. "catalogId": 2,
  11. "catalogFid": 0,
  12. "catalogName": "版权信息",
  13. "child": [],
  14. "anchor": ""
  15. },
  16. {
  17. "catalogId": 3,
  18. "catalogFid": 0,
  19. "catalogName": "第1章",
  20. "child": [
  21. {
  22. "catalogId": 301,
  23. "catalogFid": 3,
  24. "catalogName": "第1章第1节",
  25. "child": [
  26. {"catalogId": 30101, "catalogFid": 301, "catalogName": "第1章第1节第1段", "child": [], "anchor": ""},
  27. {"catalogId": 30102, "catalogFid": 301, "catalogName": "第1章第1节第2段", "child": [], "anchor": ""},
  28. {"catalogId": 30103, "catalogFid": 301, "catalogName": "第1章第1节第3段", "child": [], "anchor": ""},
  29. {"catalogId": 30104, "catalogFid": 301, "catalogName": "第1章第1节第4段", "child": [], "anchor": ""}
  30. ],
  31. "anchor": ""
  32. },
  33. {
  34. "catalogId": 302,
  35. "catalogFid": 3,
  36. "catalogName": "第1章第2节",
  37. "child": [
  38. {"catalogId": 30201, "catalogFid": 302, "catalogName": "第1章第2节第1段", "child": [], "anchor": "sec01"},
  39. {"catalogId": 30202, "catalogFid": 302, "catalogName": "第1章第2节第2段", "child": [], "anchor": "sec02"},
  40. {"catalogId": 30203, "catalogFid": 302, "catalogName": "第1章第2节第3段", "child": [], "anchor": "sec03"},
  41. {"catalogId": 30204, "catalogFid": 302, "catalogName": "第1章第2节第4段", "child": [], "anchor": "sec04"}
  42. ],
  43. "anchor": ""
  44. },
  45. {
  46. "catalogId": 303,
  47. "catalogFid": 3,
  48. "catalogName": "第1章第3节",
  49. "child": [],
  50. "anchor": ""
  51. },
  52. {
  53. "catalogId": 304,
  54. "catalogFid": 3,
  55. "catalogName": "第1章第4节",
  56. "child": [],
  57. "anchor": ""
  58. },
  59. {
  60. "catalogId": 305,
  61. "catalogFid": 3,
  62. "catalogName": "第1章第5节",
  63. "child": [],
  64. "anchor": ""
  65. }
  66. ],
  67. "anchor": ""
  68. },
  69. {
  70. "catalogId": 4,
  71. "catalogFid": 0,
  72. "catalogName": "第2章",
  73. "child": [],
  74. "anchor": ""
  75. },
  76. {
  77. "catalogId": 6,
  78. "catalogFid": 0,
  79. "catalogName": "后记",
  80. "child": [],
  81. "anchor": ""
  82. }
  83. ]

2. javaScrip:

  1. function genLi(name, catalogId, anchor, catalogFid) {
  2. return `
  3. <li>
  4. <a href="${anchor?catalogFid:catalogId}.xhtml${anchor?'#'+anchor:''}">${name}(${anchor?catalogFid:catalogId}.xhtml${anchor?'#'+anchor:''})</a>
  5. </li>
  6. `.trim();
  7. }
  8. function loopArray (arr) {
  9. let $ul = $('<ul>');
  10. arr.forEach(function(v){
  11. let $li = $(genLi(v.catalogName, v.catalogId, v.anchor, v.catalogFid));
  12. $ul.append($li);
  13. if (v.child && v.child.length) {
  14. $ul.append(loopArray(v.child));
  15. }
  16. });
  17. return $ul.get(0).outerHTML;
  18. }
  19. loopArray(newDirList);

3. 生成出的HTML代码:

  1. <ul>
  2. <li>
  3. <a href="1.xhtml">目录(1.xhtml)</a>
  4. </li>
  5. <li>
  6. <a href="2.xhtml">版权信息(2.xhtml)</a>
  7. </li>
  8. <li>
  9. <a href="3.xhtml">第1章(3.xhtml)</a>
  10. </li>
  11. <ul>
  12. <li>
  13. <a href="301.xhtml">第1章第1节(301.xhtml)</a>
  14. </li>
  15. <ul>
  16. <li>
  17. <a href="30101.xhtml">第1章第1节第1段(30101.xhtml)</a>
  18. </li>
  19. <li>
  20. <a href="30102.xhtml">第1章第1节第2段(30102.xhtml)</a>
  21. </li>
  22. <li>
  23. <a href="30103.xhtml">第1章第1节第3段(30103.xhtml)</a>
  24. </li>
  25. <li>
  26. <a href="30104.xhtml">第1章第1节第4段(30104.xhtml)</a>
  27. </li>
  28. </ul>
  29. <li>
  30. <a href="302.xhtml">第1章第2节(302.xhtml)</a>
  31. </li>
  32. <ul>
  33. <li>
  34. <a href="302.xhtml#sec01">第1章第2节第1段(302.xhtml#sec01)</a>
  35. </li>
  36. <li>
  37. <a href="302.xhtml#sec02">第1章第2节第2段(302.xhtml#sec02)</a>
  38. </li>
  39. <ul>
  40. <li>
  41. <a href="3020201.xhtml">第1章第2节第2段第1行(3020201.xhtml)</a>
  42. </li>
  43. <li>
  44. <a href="3020202.xhtml">第1章第2节第2段第2行(3020202.xhtml)</a>
  45. </li>
  46. <li>
  47. <a href="3020203.xhtml">第1章第2节第2段第3行(3020203.xhtml)</a>
  48. </li>
  49. <li>
  50. <a href="3020204.xhtml">第1章第2节第2段第4行(3020204.xhtml)</a>
  51. </li>
  52. </ul>
  53. <li>
  54. <a href="302.xhtml#sec03">第1章第2节第3段(302.xhtml#sec03)</a>
  55. </li>
  56. <li>
  57. <a href="302.xhtml#sec04">第1章第2节第4段(302.xhtml#sec04)</a>
  58. </li>
  59. </ul>
  60. <li>
  61. <a href="303.xhtml">第1章第3节(303.xhtml)</a>
  62. </li>
  63. <li>
  64. <a href="304.xhtml">第1章第4节(304.xhtml)</a>
  65. </li>
  66. <li>
  67. <a href="305.xhtml">第1章第5节(305.xhtml)</a>
  68. </li>
  69. </ul>
  70. <li>
  71. <a href="4.xhtml">第2章(4.xhtml)</a>
  72. </li>
  73. <li>
  74. <a href="6.xhtml">后记(6.xhtml)</a>
  75. </li>
  76. </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. php缓存加速优化--Xcache

    1.安装软件:cd /usr/local/src/下载软件包wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache- 3.2.0.tar.b ...

  2. 小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理

    笔记 7.Eureka服务注册中心配置控制台问题处理     简介:讲解服务注册中心管理后台,(后续还会细讲) 问题:eureka管理后台出现一串红色字体:是警告,说明有服务上线率低 EMERGENC ...

  3. 模型压缩-Learning Efficient Convolutional Networks through Network Slimming

    Zhuang Liu主页:https://liuzhuang13.github.io/ Learning Efficient Convolutional Networks through Networ ...

  4. [ML] Feature Selectors

    SparkML中关于特征的算法可分为:Extractors(特征提取).Transformers(特征转换).Selectors(特征选择)三部分. Ref: SparkML中三种特征选择算法(Vec ...

  5. openstack思维导图

    RABBITMQ memcache keystone glance nova neutron cinder horizon

  6. Expecting "jsp:param" standard action with "name" and "value" attributes

    浏览器访问报如下jsp标签错误: 根据提示,定位到jsp页面124行,代码如下: 查找原因,当<jsp:include></jsp:include>标签中没有参数时,不允许有空 ...

  7. launchImage设置后在启动时无法显示

    有人问我他的APP设置了启动页,然后居然不显示.....我觉得应该不可能啊,然后我自己再次实现了一下设置启动页,这个问题好像以前从来没有注意过,也没有很深刻的掌握APP启动页的设置和注意事项,今天遇到 ...

  8. lumen怎么得到当前Uri的控制器、Action、路由规则

    <?php namespace App\Http\Controllers; class HelloController extends Controller { public function ...

  9. 4. Linux管理命令

    df.du:磁盘.空间相关的命令 df -h 以直观的方式显示磁盘分区使用状况 df test 查询test属于哪个分区   du du -h 以直观方式显示文件夹目录的使用状况 du -s 只显示当 ...

  10. 【Hadoop】Hadoop的数据压缩方式

    概述 ​ 压缩技术能够有效减少底层存储系统(HDFS)读写字节数.压缩提高了网络带宽和磁盘空间的效率.在Hadoop下,尤其是数据规模很大和工作负载密集的情况下,使用数据压缩显得非常重要.在这种情况下 ...