2011-08-01 42 views
0

jfiddle:http://jsfiddle.net/ZmDs2/1/ 嗨我試圖完成合並其他父項目,而只打開一個與jQuery切換。jQuery:toogle - >只打開一個並摺疊其他兄弟項目

我試圖

var index = $(this).index(); 
$('.togglebox').eq(index).slideToggle("fast").siblings('.togglebox').hide(); 

我想你們幫助.. 感謝

+0

所以,如果'item1'是開放的,'item2'被點擊時,你想'item1'關閉,'item2'打開? –

+0

是的,這就是我想要的,謝謝你的解釋 – neverlate

回答

1

我改變了javascript來this

$(document).ready(function(){ 
    //Hide the tooglebox when page load 
    var current, toggleBoxes = $(".togglebox").hide(); 
    //slide up and down when click over heading 2 
    $("h3").click(function(){ 
     current = $(this).next(".togglebox"); 
     toggleBoxes.not(current).slideUp('fast'); 
     current.slideDown("fast"); 
    }); 
}); 

我只需保存所有元素的「 togglebox「類並在執行slideToggle動畫之前摺疊它們。

+0

老兄,這幾乎和我的O_O一樣,但是你在1秒內打敗了我:P –

0

試試這個

$(function(){ 
    $(".togglebox").hide(); 

    $("h3").click(function(){ 
     $(".togglebox").slideUp('fast'); 
     $(this).next(".togglebox").slideDown("fast"); 
    }); 
}); 
相關問題