2013-07-01 36 views
-1

首先我是一個完整的jQuery啓動,我需要的是修改腳本。現在我有子菜單,當我打開它並打開鏈接時,它崩潰了,我想要的是當我打開子菜單項鍊接時,子菜單仍然會在頁面實時打開時,是否有可能jquery垂直菜單崩潰isue

這裏是鏈接菜單腳本我使用http://thecodeplayer.com/walkthrough/vertical-accordion-menu-using-jquery-css3

/*jQuery time*/ 
$(document).ready(function(){ 
    $("#accordian h3").click(function(){ 
     //slide up all the link lists 
     $("#accordian ul ul").slideUp(); 
     //slide down the link list below the h3 clicked - only if its closed 
     if(!$(this).next().is(":visible")) 
     { 
      $(this).next().slideDown(); 
     } 
    }) 
}) 
+0

和你的代碼是? – slash197

+0

p.s 我嘗試了$(this).attr(「class」,「active」);但它只能工作,直到頁面刷新 – Raimonds

+1

您需要使用Cookie來保持打開狀態。 [jquery-cookie](https://github.com/carhartl/jquery-cookie) – bitWorking

回答

1

我寧願添加和刪除類

$(document).ready(function(){ 
     $("#accordian").each(function(position){ 
      var container = $(this); 
      var containerId = container.attr("id"); 
      if (! containerId){ 
        container.attr("id", position); 
        containerId = position; 
      } 
      if ($.cookie('expanded_'+ containerId) == 'true') 
       container.addClass("expanded"); 
     }); 
     $("#accordian h3").click(function(){ 
      //slide up all the link lists 
      var title = $(this); 
      var container = title.closest("div"); 
      var containerId = container.attr("id"); 

      if (container.hasClass("expanded")){ 
       container.children("ul").slideUp(); 
       container.removeClass("expanded"); 
       $.cookie('expanded_'+ containerId,'false') 
      } else { 
       container.children("ul").slideDown(); 
       container.addClass("expanded"); 
       $.cookie('expanded_'+ containerId,'true') 
      }  
     }) 
    }) 

的頁面重載問題是與餅乾http://lineadecodigo.com/jquery/usando-cookies-con-jquery/

此外,我會建議做一個類「手風琴」而不是ID

+0

這不是頁面重裝問題的答案 – bitWorking

+0

確實,現在它是 – Edorka

+0

@redreggae做喲看到任何值得反對票的答案? – Edorka

0

您可以使用cookie,但我建議不要使用它們來實現這種簡單的功能。

只需在URL中添加一個哈希標籤並使菜單對其做出反應。

當您單擊事件火災,消防setHash像這樣的字符串:setHash( 「任務」)

function setHash(str) 
{ 
    try 
    { 
     window.location.hash=str; 
    } 
    catch(err){/*if you want error reporting add it here*/} 
} 

然後用此檢查:

if(window.location.hash == something) 
{ 
    //open menu here 
} 

我還沒有加入代碼爲手風琴,但你應該有權訪問它的API。 另外要注意,jQueryUI的有爆發所有功能的手風琴:)

http://jqueryui.com/accordion/

使用jQueryUI的手風琴可能是作爲一個初學者更容易,因爲大部分的工作已經爲你做。

+1

也請注意新的cookie歐盟規則,如果它適用或有興趣。 http://www.theeucookielaw.com/如果你這樣搖擺:) – Eirinn