事实证明这比看起来要复杂一些,因为Accordion API 文档并不是那么容易理解,而且该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);