开发者

Simple jQuery Toggle of children

开发者 https://www.devze.com 2022-12-25 02:28 出处:网络
Um, why no worky? Trying to toggle a ul with this: $(\"#others\").children(\".btn\").click(function(){

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">
 <d开发者_运维知识库iv 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>


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();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消