1

我有一个 Accordion,我想通过其 ID 获取特定标头的索引。

手风琴由中继器生成:

<asp:Repeater ID="rptProjectList" OnItemDataBound="rptProjects_ItemDataBound" runat="server">
    <HeaderTemplate>
        <div id="accordion">
    </HeaderTemplate>

    <ItemTemplate>
        <h1 style="margin: 0px" class="accordionHeader" id='<%# Eval("projectCode") %>'></h1>

        <div>
           ....some stuff             
        </div>
    </ItemTemplate>

    <FooterTemplate>
        </div>
    </FooterTemplate>
</asp:Repeater>

给定 projectCode 作为参数,我希望能够打开扩展特定部分的页面。例如,用户转到 MyPage.aspx?project=ABC123,页面加载时 ABC123 项目打开,其他面板折叠。

我拥有的 jQuery 是:

var activeProject = $(document).getUrlParam("project");

获取代码。

$j('#accordion').accordion({
        active: activeIndex,
        collapsible: true
    });

设置手风琴。

我无法弄清楚的是中间的那一点。我需要能够通过遍历 div 来获取 activeIndex 以获取具有相关项目代码的行的索引。

4

1 回答 1

6

你可以这样做。 手风琴

var active = $( ".selector" ).accordion( "option", "active" ); //getter 



<div id="accordion">
  <h3 class='headAcc'>First header</h3>
  <div>First content panel</div>
  <h3 class='headAcc'>Second header</h3>
  <div>Second content panel</div>
</div>

jQuery

$('#accordion').accordion({
});

//suppose you want to show thee index which starts some text 
var indexToActivate = $('.headAcc:contains("Second header")').index();
alert(indexToActivate);
indexToActivate=indexToActivate-1; //index is zero based for jquery ui. 
$('button').on('click',function(){
    var active = $( "#accordion" ).accordion( "option", "active" ); //getter 
    alert('Current Index is ' + active +" ");

     $( "#accordion" ).accordion( "option", "active",indexToActivate ); //setter

     var active = $( "#accordion" ).accordion( "option", "active" ); //getter 
       alert('Current New Index is ' + active +" ");
});

Working Demo

于 2013-03-11T10:55:33.907 回答