选择所有“li”不包含“ul”之内
我正在为产品构建嵌套下拉导航,我想自动隐藏任何空类别。
不幸的是,服务器端语言不允许有效的方法来做到这一点,所以我想我可以直接输出每个类别拥有的产品数量,然后使用 jQuery 删除任何空节点。
我只想定位 nav#top_nav
中的 li
:
<nav id="top_nav">
<nav class="nav1">
<ul>
<li data-num-products="0">
<a href="...">AAA</a>
<ul>
<li data-num-products="3"><a href="...">BBB</a></li>
<li data-num-products="0"><a href="...">CCC</a></li>
</ul>
</li>
<li data-num-products="7"><a href="">DDD</a></li>
</ul>
<nav>
</nav>
给定 1 层嵌套 ul
,我想删除任何 < code>li 是...
其中没有嵌套的
ul
并且有
data-num-products == 0 。
因此,在上面的示例中,AAA
被保留,因为它有 ul
子级,但 CCC
被删除,因为它没有 ul
> 儿童和无产品。
更新:
可能需要 2 次删除,因为如果 li 包含 ul,其 li 元素全部被删除,那么我们也想删除 ul。
I'm building a nested drop down navigation for products and I want to automatically hide any categories that are empty.
Unfortunately server side language does not allow efficient way of doing this, so I thought I could output number of products each category has directly, then use jQuery to remove any empty nodes.
I want to target only the li
's within nav#top_nav
:
<nav id="top_nav">
<nav class="nav1">
<ul>
<li data-num-products="0">
<a href="...">AAA</a>
<ul>
<li data-num-products="3"><a href="...">BBB</a></li>
<li data-num-products="0"><a href="...">CCC</a></li>
</ul>
</li>
<li data-num-products="7"><a href="">DDD</a></li>
</ul>
<nav>
</nav>
Given 1 level of nesting ul
's, I want to remove any li
's that...
have no nested
ul
's within them andhave
data-num-products == 0
.
So in the example above AAA
is retained because it has ul
children, but CCC
is removed because it has no ul
children and no products.
UPDATE:
It might require 2 passes of removal because if a li contains a ul whose li elements are all removed, then we'll want to remove the ul too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://jsfiddle.net/X2D7y/
仅当没有 UL 后代且具有属性值时才会删除共 0 个
http://jsfiddle.net/X2D7y/
This will only remove if there are no UL descendants AND have attribute value of 0
像这样:
选择器细分是...
关于您更新的问题,只需将
:not(:has(ul))
更改为:not(:has(li))
。http://jsfiddle.net/4YFDd/
Like this:
The selector breakdown is...
Regarding your updated question, just change
:not(:has(ul))
to:not(:has(li))
.http://jsfiddle.net/4YFDd/
在 Javascript 中:
演示: http://jsfiddle.net/ThinkingStiff/2zS9B/
In Javascript:
Demo: http://jsfiddle.net/ThinkingStiff/2zS9B/