我有一个像这样的多级列表:
<ul>
<li>Item 1
<ul>
<li>Item 1's 1st Child
<ul>
<li>Item 1's 1st Grandchild
<ul>
<li>Item 1's Grand Grandchild</li>
</ul>
</li>
<li>Item 1's 2nd Grandchild</li>
<li>Item 1's 3rd Grandchild</li>
</ul>
</li>
<li>Item 1's 2nd Child</li>
<li>Item 1's 3rd Child</li>
</ul>
</li>
<li>Item 2</li>
</ul>
我希望每个li
人都根据他们的职位拥有班级“水平”。结果将是这样的:
<ul>
<li class="level-1">Item 1
<ul>
<li class="level-2">Item 1's 1st Child
<ul>
<li class="level-3">Item 1's 1st Grandchild
<ul>
<li class="level-4">Item 1's Grand Grandchild</li>
</ul>
</li>
<li class="level-3">Item 1's 2nd Grandchild</li>
<li class="level-3">Item 1's 3rd Grandchild</li>
</ul>
</li>
<li class="level-2">Item 1's 2nd Child</li>
<li class="level-2">Item 1's 3rd Child</li>
</ul>
</li>
<li class="level-1">Item 2</li>
</ul>
有没有办法用 jQuery 实现这一点?
非常感谢您的关注。