小编Lun*_*ter的帖子

jQuery - 解析DOM并创建树结构(复合模式)

我正在解析DOM,并且我想要将与某个选择器匹配的所有元素的层次结构(例如,提取)提取$('[data-node]')到JavaScript对象树结构中.我没有找到标准的jQuery方法.使用jQuery.find()似乎返回一个平面列表.也许我刚刚错过了一些明显的东西?

例如,我想解析一下:

<div data-node="root">
   <div class="insignificant-presentation">
        <div>
            <div data-node="interestingItem">
            </div>
        </div>
    <div>
    <div data-node="anotherInterestingItem">
        <div data-node="usefulItem">
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

并在JavaScript中创建一个结构,如下所示:

[
  {
    "node": "root",
    "children": [
      {
        "node": "interestingItem",
        "children": []
      },
      {
        "node": "anotherInterestingItem",
        "children": [
          {
            "node": "usefulItem",
            "children": []
          }
        ]
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

javascript jquery composite

2
推荐指数
1
解决办法
852
查看次数

标签 统计

composite ×1

javascript ×1

jquery ×1