简体   繁体   中英

How to select elements in jQuery that don't have a specific element in it

How to select elements in jQuery that don't have a specific element in it. I want to make a sidebar which will close after the user either click the menu or the sidebar itself but limited to dropdown menu. Here's the structure code of the menu:

<li><a href="#">Menu Normal</a></li>
<li>
  <a href="#">Menu Dropdown</a>
  <ul class="sidebar-nav-child">
    <li><a href="#">Menu-1</a></li>
    <li><a href="#">Menu-1</a></li>
    <li><a href="#">Menu-1</a></li>
  </ul>
</li>

Here's the jquery:

$(".sidebar-toggle").click(function(e){
  e.preventDefault();
  $("#wrapper").addClass('sidebar-show');
  $("#sidebar, .sidebar-nav li:not(:has(.sidebar-nav-child)) a").click(function(){
    $("#wrapper").removeClass('sidebar-show');
  });
});

My code above will still close the sidebar even though i click the dropdown. Thank you in advance.

You need to process li that are direct children of .sidebar-nav and don't contain nested ul :

 $("#sidebar, .sidebar-nav > li:not(:has(ul)) a")

But I haven't get clear what role of #sidebar here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM