手风琴标签不会崩溃

时间:2015-01-08 08:33:21

标签: javascript jquery

我不熟悉javascript,但是我希望打开的标签在点击“ - ”符号时再次折叠,

示例链接:http://www.dev.redefinegraphicstudio.com/acp/SLOCPI%20Mobile/myclients-life-07.html

$.fn.expCollapse = function(){ 
    $mainContainer = $(this);
    $mainContainer.find('.acco-head').on('click',function(){
        $(this).parent().parent().find('.acco').each(function(){
            $(this).removeClass('active');
            $(this).find('.acco-content').stop().slideUp(500);

        });
        $(this).parent().addClass('active');
        $containerToOpen = $(this).next('.acco-content');
        $containerToOpen.stop().slideDown(700);

    });
}

这是代码:

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用slideToggle代替slideDown

$mainContainer.find('.acco-head').on('click',function(){
    $('.acco').each(function(){ // don't make hierarchy, as you need to slideUp all contents
        $(this).removeClass('active');
        $(this).find('.acco-content').stop().slideUp(500);    
    });
    $(this).parent().addClass('active');
    $containerToOpen = $(this).next('.acco-content');
    $containerToOpen.stop().slideToggle(700);  // use slideToggle  
});



$.fn.expCollapse = function () {
  $mainContainer = $(this);
  $mainContainer.find('.acco-head').on('click', function () {
    $('.acco').each(function () { // don't make hierarchy, as you need to slideUp all contents
      $(this).removeClass('active');
      $(this).find('.acco-content').stop().slideUp(500);
    });
    $acco=$(this).closest('.acco')
   
    $containerToOpen = $(this).next('.acco-content');
    $containerToOpen.stop().slideToggle(700,function(){
       if($containerToOpen.is(':visible')){$acco.addClass('active')}
       else{$acco.removeClass('active')}
    }); // use slideToggle
   
    
  });
};
$('.acco-group').expCollapse();

<link href="http://www.dev.redefinegraphicstudio.com/acp/SLOCPI%20Mobile/styles/styles.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="acco-group">
  <div class="acco active">
    <h4 class="acco-head">Contact Information <span><!-- --></span></h4>
    <div class="acco-content" style="display: none; height: 606px; padding-top: 15px; margin-top: 0px; padding-bottom: 15px; margin-bottom: 0px;">
      <table class="align-formatted">
        <tbody>
          <tr>
            <td>Home Phone</td>
            <td>123123123</td>
          </tr>
          <tr>
            <td>Mobile Phone</td>
            <td>123123123</td>
          </tr>
          <tr>
            <td>Business Phone</td>
            <td>213123123123</td>
          </tr>
          <tr>
            <td>Email Address</td>
            <td>jdc@gmail.com</td>
          </tr>
          <tr>
            <td>Residence Address</td>
            <td>Makati Area</td>
          </tr>
          <tr>
            <td>City</td>
            <td>Makati</td>
          </tr>
          <tr>
            <td>Province/State</td>
            <td>Metro Manila</td>
          </tr>
          <tr>
            <td>Country</td>
            <td>Philippines</td>
          </tr>
          <tr>
            <td>ZIP Code</td>
            <td>1900</td>
          </tr>
          <tr>
            <td>Business Address</td>
            <td>Makati Area</td>
          </tr>
          <tr>
            <td>City</td>
            <td>Makati</td>
          </tr>
          <tr>
            <td>Province/State</td>
            <td>Metro Manila</td>
          </tr>
          <tr>
            <td>Country</td>
            <td>Philippines</td>
          </tr>
          <tr>
            <td>ZIP Code</td>
            <td>1900</td>
          </tr>
        </tbody>
      </table>

    </div>
  </div>

  <div class="acco">
    <h4 class="acco-head">Employment Information<span><!-- --></span></h4>
    <div class="acco-content" style="">
      <table class="align-formatted">
        <tbody>
          <tr>
            <td>Occupation</td>
            <td>President</td>
          </tr>
          <tr>
            <td>Employer Name</td>
            <td>ABC Corp</td>
          </tr>

        </tbody>
      </table>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;