选择所有“li”不包含“ul”之内

发布于 2024-12-27 19:44:12 字数 1092 浏览 4 评论 0原文

我正在为产品构建嵌套下拉导航,我想自动隐藏任何空类别。

不幸的是,服务器端语言不允许有效的方法来做到这一点,所以我想我可以直接输出每个类别拥有的产品数量,然后使用 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 and

  • have 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

腹黑女流氓 2025-01-03 19:44:12
$( "#top_nav li").filter( function(){
    return !this.getElementsByTagName("ul").length && !+this.getAttribute("data-num-products");
}).remove();

http://jsfiddle.net/X2D7y/

仅当没有 UL 后代且具有属性值时才会删除共 0 个

$( "#top_nav li").filter( function(){
    return !this.getElementsByTagName("ul").length && !+this.getAttribute("data-num-products");
}).remove();

http://jsfiddle.net/X2D7y/

This will only remove if there are no UL descendants AND have attribute value of 0

堇年纸鸢 2025-01-03 19:44:12

像这样:

$('#top_nav li[data-num-products="0"]:not(:has(ul))').remove();

选择器细分是...

'#top_nav'                // start at element with "top_nav" id
' '                       // and select descendant...
'li'                      // li elements...
'[data-num-products="0"]' //   ...where attribute "data-num-products" is "0"
':not('                   //   ...but exclude li elements that...
  ':has(ul)'              //        ...have descendant ul elements
')'

关于您更新的问题,只需将 :not(:has(ul)) 更改为 :not(:has(li))

$('#top_nav li[data-num-products="0"]:not(:has(li))').remove();

http://jsfiddle.net/4YFDd/

Like this:

$('#top_nav li[data-num-products="0"]:not(:has(ul))').remove();

The selector breakdown is...

'#top_nav'                // start at element with "top_nav" id
' '                       // and select descendant...
'li'                      // li elements...
'[data-num-products="0"]' //   ...where attribute "data-num-products" is "0"
':not('                   //   ...but exclude li elements that...
  ':has(ul)'              //        ...have descendant ul elements
')'

Regarding your updated question, just change :not(:has(ul)) to :not(:has(li)).

$('#top_nav li[data-num-products="0"]:not(:has(li))').remove();

http://jsfiddle.net/4YFDd/

虐人心 2025-01-03 19:44:12
$("#top_nav li[data-num-products='0']").filter(function() { 
    return $(this).children("ul").length === 0; 
}).remove();
$("#top_nav li[data-num-products='0']").filter(function() { 
    return $(this).children("ul").length === 0; 
}).remove();
禾厶谷欠 2025-01-03 19:44:12

在 Javascript 中:

var lis = document.querySelectorAll( '#top_nav li[data-num-products="0"]' );
for( var li = 0; li < lis.length; li++ ) { 
    !lis[li].getElementsByTagName( 'ul' ).length && lis[li].parentNode.removeChild( lis[li] );
};

演示: http://jsfiddle.net/ThinkingStiff/2zS9B/

In Javascript:

var lis = document.querySelectorAll( '#top_nav li[data-num-products="0"]' );
for( var li = 0; li < lis.length; li++ ) { 
    !lis[li].getElementsByTagName( 'ul' ).length && lis[li].parentNode.removeChild( lis[li] );
};

Demo: http://jsfiddle.net/ThinkingStiff/2zS9B/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文