// html
<div id="accordion" >
<h3 class='headAcc' id="head_1">First header</h3>
<div>First content panel</div>
<h3 class='headAcc'id="head_2">Second header</h3>
<div>Second content panel</div>
</div>
//javascript
$('#accordion').accordion({collapsible:true,active:false});
问题:所有选项卡默认关闭。所以我需要使用标题元素 id 获取选项卡的索引。我怎样才能做到这一点。
我试过跟随。但没有运气。提前致谢。
var indexOfheaderOne= $('h3#head_1').index(); //returns 0 which is ok
var indexOfheaderTwo= $('h3#head_2').index(); // returns 2 instead of 1.
//I think the reason is it will count the indexes based on all sibling elements
//not just from header elements. Is there any workaround for this.
编辑
@Thusar 解决方案的小修改
假设您的 html 包含更多<h3>
手风琴之外的元素。然后以下工作将适用于该类型的场景。
HTML
<h3 id="test1">Example Head 1</h3>
<h3 id="test2">Example Head 2</h3>
<h3 id="test3">Example Head 3</h3>
<div id="accordion" >
<h3 class='headAcc' id="head_1">First header</h3>
<div>First content panel</div>
<h3 class='headAcc'id="head_2">Second header</h3>
<div>Second content panel</div>
</div>
JavaScript
alert($('h3#head_1').index('h3.headAcc'));//return 0 as expected
alert($('h3#head_2').index());//return 2 because element is in after first tab div
alert($('h3#head_2').index('h3.headAcc'));//return 1 as expected