// 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>
// 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
答案 0 :(得分:2)
var indexOfheaderTwo= $('h3#head_2').index('h3'); //returns 1 as index of h3 with respect to parent is traced and it is the 2nd child of parent with tag h3.
索引从0开始。
问题的解释。
var indexOfheaderOne= $('h3#head_1').index(); //returns 0 As it is first child of parent div
var indexOfheaderTwo= $('h3#head_2').index(); // returns 2 As it is third child of parent div
阅读.index()
答案 1 :(得分:1)
你对它返回2的假设是正确的。
将索引选择器更改为:
var indexOfheaderOne= $('h3').index($('#head_1')); //returns 0 which is ok
var indexOfheaderTwo= $('h3').index($('#head_2')); // returns 1.
JsFiddle:http://jsfiddle.net/GxQ3c/2/
答案 2 :(得分:0)
$("#accordion").accordion("option", "active", i);
你试试这个吗?手风琴可能是内置功能。</ p>
尝试演示this。