// HTMLjquery的手風琴標籤索引使用頭部元素ID
<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>
// java描述
$('#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
真棒兄弟。這就是我想要的。 – Viraths
@Viraths歡迎:) :)高興地幫助:) –