On click slidetoggle div but hide others first - jQuery

后端 未结 5 1831
太阳男子
太阳男子 2021-02-06 13:06

I have a page that when a user clicks a title, the following div toggles display.

I want to somehow say if any other divs are display:block then set them to display non

相关标签:
5条回答
  • 2021-02-06 13:36

    Try like below... it will work...

    Fiddle : http://jsfiddle.net/RYh7U/83/

        $(document).ready(function () {
            $('.office-title').next('div').slideToggle();
            $('.office-title').click(function(){   
            $('.office-title').next('div').slideUp();
            $(this).next('div').slideToggle(); 
            return false;
            });
         });
    
    0 讨论(0)
  • 2021-02-06 13:38
    $('.office-title').click(function(){
         $(this).next('div').slideToggle();
         $(this).parent(".office-row").siblings().hide(); // this should help
         return false;
    });
    
    0 讨论(0)
  • 2021-02-06 13:53

    </office> is not a valid closing. The closing should be </div>

    <div class="office-row">
            <h3 class="office-title">Title</h3>
            <div class="office">sadasd</div>
          </div>
        <div class="office-row">
            <h3 class="office-title">Title</h3>
            <div class="office">sadasd</div>
        </div>
        <div class="office-row">
            <h3 class="office-title">Title</h3>
            <div class="office">sadasd</div>
        </div>
    

    CSS:

    .office
    {
        display: none;
    }
    

    and jquery:

    $(function () {
        $('.office-title').click(function () {
            $(this).next('div').slideToggle();
    
            $(this).parent().siblings().children().next().slideUp();
            return false;
        });
    });
    

    HERE YOU CAN CHECK

    0 讨论(0)
  • 2021-02-06 13:54

    This should do it: http://jsfiddle.net/gKgJ7/

    $('.office-title').click(function(){
       $('.office').slideUp();
       $(this).next('div').slideToggle();
       return false;
    });
    

    note:

    This is not a valid closing:

    <div class="office">sadasd</office>
       //---------------------^^^^^^^^----should be </div>
    
    0 讨论(0)
  • 2021-02-06 13:58

    This will close all the open div with animation and opens the required div

    $('.office-title').click(function(){
         var that = $(this);
         if('.office').each(function() {
           if($(this) == $(that).next('div')) {
              $(this).slideToggle();
           }
           else {
              if($(this).css('display')!=='none') {
                 $(this).slideToggle();
              }
           }
         });
         return false;
    });
    
    0 讨论(0)
提交回复
热议问题