2012-04-30 102 views
1

試圖創建鏈接到包含摺疊的手風琴項目的頁面,每個項目由div ID編號標識。如何創建摺疊摺疊手風琴項目的鏈接?

失敗的嘗試通過添加參數,以我的鏈接鏈接,打開特定項目的樣子:

sample.html#itemIdX // opens to the page but not the item 

sample.html?itemIdX // same result 

的項目使用H3類:

itemPreviewTitle accordionHeader ui-accordion-header ui-helper-reset ui-state-default ui-corner-all" role="tab" aria-expanded="false" aria-selected="false" tabindex="-1" style="zoom: 1; 

如何創建一個鏈接,使我的itemX有擴展狀態?

+0

你有沒有運氣在我的答案中應用解決方案? –

回答

0

這看起來有點棘手,因爲Accordion API docs並不容易,activate()功能看起來不像廣告那樣工作。

從您的問題,它聽起來像你想通過引用div ID打開手風琴部分。這是不可能的,您只能使用基於0的索引來標識節(例如,0 =第一節,1 =第二節等)。

話雖如此,這種做法將工作:

這樣定義鏈接:

<a href="10387904_Accordion_link_2.html?openAccordionId=0">Open first item</a> 
<a href="10387904_Accordion_link_2.html?openAccordionId=1">Open second item</a> 
<a href="10387904_Accordion_link_2.html?openAccordionId=2">Open third item</a> 

在包含手風琴的頁面,使用下面的代碼來提取查詢字符串的ID和初始化有關部分的手風琴激活:

// Using the parseQueryString extension from 
// http://paulgueller.com/2011/04/26/parse-the-querystring-with-jquery/ 
$.extend({ 
    parseQuerystring: function() { 
     var nvpair = {}; 
     var qs = window.location.search.replace('?', ''); 
     var pairs = qs.split('&'); 
     $.each(pairs, function (i, v) { 
      var pair = v.split('='); 
      nvpair[pair[0]] = pair[1]; 
     }); 
     return nvpair; 
    } 
}); 

// Get the index of the section we want to open from the querystring. 
var openAccordionId = parseInt($.parseQuerystring()["openAccordionId"]); 

// Initialise the accordion with the active section defined. 
var accordion = $("#accordion").accordion({ active: openAccordionId }); 

// Note: for some reason, the following does not work: 
// var accordion = $("#accordion").accordion(); 
// accordion.activate(openAccordionId);