2014-02-24 40 views
0

我一次只想展開/摺疊一個容器。當我點擊第一個容器時,第二個容器應該摺疊,當第二個容器展開時,第一個容器應該自動摺疊。以下是小提琴。請指導...謝謝展開/收起問題

http://jsfiddle.net/awaises/eK8X5/1138/

jQuery 

$(".header").click(function() { 
    $header = $(this); 
    $content = $header.next();  
    $content.slideToggle(500, function() { 
     $header.text(function() { 
      return $content.is(":visible") ? "Collapse" : "Expand"; 
     }); 
    }); 
}); 
+0

爲什麼不使用手風琴的文字? http://jqueryui.com/accordion/ – phron

回答

2

您可以將此行添加到您單擊處理程序:

$(".header").not(this).text('Expand').next().slideUp(); 

Updated Fiddle

0

你可以找到任何擴展的content元素然後崩潰它像

$('.content').not($content).stop(true).filter(':visible').slideUp(500, function(){ 
    $(this).prev().text('Expand') 
}) 

演示:Fiddle

0

使用此

$(".content").not($content).slideUp(); 

調用的slideToggle功能和ofcourse之前不要忘了更新的標題