2015-10-06 178 views
1

我創建一個手風琴,你可以在http://jsfiddle.net/ybgjub4g/手風琴重開

每一個手風琴的手風琴點擊當前手風琴打開關閉看到,唯一的問題是,當我點擊當前打開的手風琴,關閉它,然後它關閉並重新打開,我無法弄清楚爲什麼。

$(document).ready(function() { 
    (function($) { 
     var allPanels = $('.accordion > span').hide(); 
     $('.accordion > .question').click(function() { 
      allPanels.slideUp(); 
      $(this).next('span').slideDown(); 
      $('.accordion.current').removeClass('current'); 
      $(this).parent().addClass('current'); 
      return false; 
     }); 
    } 
    )(jQuery); 
}); 

回答

1

這裏是一個DEMO

JS:

$(document).ready(function() { 
(function($) { 
    var allPanels = $('.accordion > span'); 

    $('.accordion > .question').click(function() { 
     var _this = this; 

     $('.current > span ').slideUp() 

     if(!$(_this).parent().hasClass('current')){ 
      $(_this).next('span').slideDown(); 
      $('.accordion.current').removeClass('current'); 
      $(_this).parent().addClass('current'); 
     }else{ 

      $(_this).parent().removeClass('current'); 
     } 




     return false; 
    }); 
} 
)(jQuery); 
}); 
+0

@ 6h8j5我認爲它適合你嗎? – guvenckardas

+0

如果您點擊打開的標題,則會關閉並自動重新打開。 – bigdaveygeorge

+0

對不起,我正在嘲笑它 – guvenckardas

1

你可以試試這個解決方案

$(".question").click(function(){ 

    if(false == $(this).next().is(':visible')) { 
     $('span').slideUp(300); 
    } 
    $(this).next().slideToggle(300); 
}); 

http://jsfiddle.net/ybgjub4g/5/

0

使用固定:

$(document).ready(function() { 
    $(".accordion span").hide(); 
    $(".accordion .question").click(function(){ 
     if(false == $(this).next().is(':visible')) { 
      $('span').slideUp(300); 
     } 
     $(this).next().slideToggle(300); 
     $('.accordion.current').removeClass('current'); 
     $(this).parent().addClass('current'); 
    }); 
});