小编tre*_*rev的帖子

使用jquery生成任意深度列表

在javascript中,我有一个对象数组,代表一个任意深度的列表......

data =
[
 { title, depth },
 { title, depth },
 { title, depth }, 
 { title, depth }, 
]
Run Code Online (Sandbox Code Playgroud)

...深度是元素列表中的深度.

我想将这些数据转换为html.

例如:

[
 { title: "one", depth : 1 },
 { title: "two", depth : 1 },
 { title: "three", depth : 2 }, 
 { title: "four", depth : 3 }, 
 { title: "five", depth : 1 }, 
]
Run Code Online (Sandbox Code Playgroud)

成为...

<ul>
  <li><p>one</p></li>
  <li>
    <p>two</p>
    <ul>
      <li>
        <p>three</p>
        <ul>
          <li><p>four</p></li> 
        </ul>
      </li>
    </ul>
  </li>
  <li><p>five</p></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

使用jQuery,最简单的方法是什么?

谢谢

javascript jquery

6
推荐指数
1
解决办法
203
查看次数

标签 统计

javascript ×1

jquery ×1