Simple jQuery Toggle of children

2019-08-03 04:25发布

Um, why no worky? Trying to toggle a ul with this:

 $("#others").children(".btn").click(function(){
    if ($(this).children("ul").is(":hidden"))
    {
   $(this).children("ul").slideDown();
    } else {
   $(this).children("ul").slideUp();
    }
 });

And:

<div id="other">
 <div id="galleries">
  <a href="#" class="btn">Galleries >> </a>
  <ul id="select_gallery">
  ...
  </ul>
 </div>
 <div id="events">
  <a href="#" class="btn">Events >> </a>
  <ul id="select_event">
  ...
  </ul>
 </div>
</div>

1条回答
forever°为你锁心
2楼-- · 2019-08-03 04:42

The UL isn't a child of the .btn its a sibling, try using next:

$("#other a.btn").click(function(){
    var ul = $(this).next("ul");
    if (ul.is(":hidden")) {
        ul.slideDown();
    } else {
        ul.slideUp();
    }
});
查看更多
登录 后发表回答