2013-10-16 32 views
0

// 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 

回答

2

DEMO

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

真棒兄弟。这就是我想要的。 – Viraths

+0

@Viraths欢迎:) :)高兴地帮助:) –

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/

+0

感谢兄弟。我知道了。 – Viraths

0
$("#accordion").accordion("option", "active", i); 

你试试这个?用手风琴它可能是内置功能。

试用演示this