2012-10-19 58 views
0

我有以下jQuery可以展開和摺疊頁面上的部分。 它還會記住哪一部分是最後展開的,並在頁面重新加載時應用。展開/收起所有jquery

我想要做的是在我的頁面上添加一個「全部展開」和「全部摺疊」鏈接,但我不確定該腳本應該是什麼樣子?有任何幫助?

<script type='text/javascript'> 
$(function() { 
    $(".toggle").hide();          //Hide .toggles 

    $(window).load(function() { 
     $('.toggle').not(':hidden').prev('.trigger').trigger("click"); 
    }); 

    $('.trigger').each(function() {       //For each .trigger 
     var theActive = $.cookie($(this).attr('id'));   //Retrieve the cookies 
     if (theActive) {          //Verify if cookies exist 
      $('#' + theActive).next(".toggle").slideDown(0); //And slide down the respective .toggle 
     } 
    }); 

    $(".trigger").toggle(          //Toggle permits alternate clicks 
    function() { 
    $(this).next('.toggle').slideDown('slow');     //On odd clicks, .toggle slides down... 
    $.cookie($(this).attr('id'), $(this).attr('id'));   //...and the cookie is set by its ids. 
    }, function() { 
    $(this).next('.toggle').slideUp('slow');     //On even clicks, .toggle slides up... 
    $.cookie($(this).attr('id'), null);       //...and the cookie is deleted. 
    }); 
}); 

第1
<div class = "toggle"> 
    Some stuff 
</div>  
<h3 class = "trigger" id="H1">Section 2</h3> 
<div class = "toggle"> 
    More stuff 
</div> 
+0

$(「。toggle」)。slideDown(); ? – Jeff

+0

試試這個..http://designgala.com/how-to-expand-collapse-toggle-div-layer-using-jquery/ – sharmila

+0

你可以有一些隱藏的變量,並把最後一項點擊和行動的價值。像'#h1;擴大'。然後在準備好的事件中檢查隱藏的值並用';'解析分隔符並根據條件 –

回答

0

除非我弄錯了,撥動類是要擴大部分,這樣做

$(".toggle").slideDown(); 

應滑落用下來的所有元素類切換。使用slideUp來摺疊所有按鈕。

+0

調用slideDown/slideUp聽起來很簡單。我最初認爲我必須自己做每一部分,但顯然不是。將嘗試一下。 –

+0

@ user1208908 - 爲您做了這個工作嗎? – Jeff

+0

還沒有。我沒有花很多時間在上面。當然,我只是錯過了一些明顯的東西 –