如何使用 jQuery 选择嵌套列表的叶元素
我只需要选择嵌套列表的叶链接元素
示例 HTML
<ul>
<li>folder
<ul>
<li>><a class="t-link" href="">folder</a>
<ul>
<li><a class="t-link" href="">leaf 1</a><li>
<li><a class="t-link" href="">leaf 2</a><li>
</ul>
</li>
</ul>
</li>
</ul>
因此,在此示例中,我想选择两个叶链接,但不选择文件夹链接 文件夹可以无限深度嵌套(但通常只有 2-3 层)。我想我需要寻找不包含 ul 元素的 li 元素,但我看不到你如何在 jQuery 中做到这一点
I need to select only the leaf link elements of a nested list
Sample HTML
<ul>
<li>folder
<ul>
<li>><a class="t-link" href="">folder</a>
<ul>
<li><a class="t-link" href="">leaf 1</a><li>
<li><a class="t-link" href="">leaf 2</a><li>
</ul>
</li>
</ul>
</li>
</ul>
So in this example I want to select the two leaf links but not the folder link
The folders can be nested infinitely deeply (but typically only 2-3 levels). I figure I need to look for li elements that don't contain ul elements but I can't see how you do that in jQuery
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将选择没有其他无序列表的叶节点作为子节点:
带有颜色编码示例的快速 jsFiddle: http://jsfiddle .net/rFuUc/
This will select the leaf nodes that do not have other unordered lists as children:
Quick jsFiddle with color coded example: http://jsfiddle.net/rFuUc/
应该有效。
should work.
尝试
DEMO
try
DEMO