我正在解析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)