简体   繁体   English

使用jQuery切换元素可见性!

[英]Toggle element visibility with jQuery!

I have the following markup: 我有以下标记:

<div class="header">Text</div>
<div class="hiddenArea">sdsada</div>

<div class="header">Text2</div>
<div class="hiddenArea">sdsada</div>

and the following jQuery: 和以下jQuery:

$('.header').click(function() {
        var t = $(this).next('.hiddenArea').slideToggle();
    });

When the hiddenArea is revealed I want to hide the other hiddenArea if it is visible? 显示hiddenArea时,我想隐藏其他hiddenArea(如果可见)吗? I want to make this so that I can add other headers and hiddenAreas if I need them. 我想这样做,以便在需要时可以添加其他标头和hiddenAreas。

Update: 更新:

Thanks Guys, ended up doing this: 谢谢你们,最终做到了:

$('#messages .header').click(function() {
  if (!$(this).next().is(':visible')) {
    $('.hiddenArea').slideToggle();
  }
});

Assuming you have one hiddenArea visible when the form is rendered this will work. 假设在呈现表单时您有一个hiddenArea可见,那么它将起作用。 Also note you dont need the filter inside the next method as next only ever gives you the next sibling. 还要注意,您不需要在next方法内使用过滤器,因为next仅会给您下一个兄弟。

$('.header').click(function() {
    var $el = $(this);
    if ( ! $el.next().is('visible') ){
       $('div.hiddenArea:visible').slideUp( function(){
           var t = $el.next().slideDown();
       });
    }
});

Thanks Guys, ended up doing this: 谢谢你们,最终做到了:

$('#messages .header').click(function() {
        if (!$(this).next().is(':visible')) {
            $('.hiddenArea').slideToggle();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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