2010-07-20 127 views
1

有史以来第一个问题在stackoverflow问。 所以,问题是:对的document.ready 两个手风琴声明(jQuery的1.4.2和jQuery UI的1.8.2):jquery嵌套式手风琴问题

 $(document).ready(function() { 
     $("#accordion").accordion({ 
      header: 'h3' 
     }); 

     $("#accordion2").accordion({ 
      header: 'h4' 
     }); 

     $(function() { 
      $(".get-index").click(function() { 
       var activecontent = $("#accordion").accordion("option", "active"); 
       alert(activecontent);     
      }); 
     }); 
    }); 

HTML:

<div id="accordion"> 
    <h3><a href="#">Section 1</a></h3> 
    <div> 
     Content Section 1: Parent 
     <div id="accordion2"> 
      <h4><a href="#">SubSection 1</a></h4> 
      <div>content section 1: child</div> 
      <h4><a href="#">SubSection 2</a></h4> 
      <div>content section 2: child</div> 
      <h4><a href="#">SubSection 3</a></h4> 
      <div>content section 3: child</div> 
      <h4><a href="#">SubSection 4</a></h4> 
      <div>content section 4: child</div> 
     </div> 
    </div> 
    <h3><a href="#">Section 2</a></h3> 
    <div> 
     Content Section 2: Parent 
    </div> 
    <h3><a href="#">Section 3</a></h3> 
    <div> 
     Content Section 3: Parent 
    </div> 
    <h3><a href="#">Section 4</a></h3> 
    <div> 
     Content Section 4: Parent 

     <button type="button" class="get-index ui-button ui-button-text-only ui-widget ui-state-default ui-corner-all"> 
      <span class="ui-button-text">index</span> 
     </button> 

    </div> 
</div> 

最后:什么是错的,为什么“activecontent”是7?我知道,有4个父面板+ 4个子面板,从0开始,它是7.但我试图获得最后父面板的索引,它应该是3.

任何帮助非常感谢。

代码发布:http://jsbin.com/eqewe

回答

3

不幸的是,这是在jQuery用户界面,in the accordion code了一个错误:

o.active = o.collapsible && clickedIsActive ? false 
    : $('.ui-accordion-header', this.element).index(clicked); 

它找到任何$('.ui-accordion-header'),不只是你指定的头选择,而不是只顾眼前利益的孩子。 我会把这个作为一个与jQuery UI家伙的bug,.active属性真的应该设置不同。我已经进入了一个错误与jQuery UI的团队为这个位置:http://dev.jqueryui.com/ticket/5841


你可以自己用.index()寻找元素,像这样的工作,围绕它现在:

$(function() { 
    $(".get-index").click(function() { 
    var a= $("#accordion").children('.ui-state-active').index('#accordion > h3'); 
    alert(a);     
    }); 
});​ 

You can try it out here

+0

非常感谢,问题得到解答。 – user397169 2010-07-20 19:49:29

+0

@msqsf - 欢迎:)请务必接受有关此问题和未来问题的答案,它有助于下一位寻找相同问题的人。我现在正在输入该错误报告,因为这是一个有效的错误,我会在有链接后更新。 – 2010-07-20 19:50:47

+0

只要给现在遇到这个问题的人提供一个参考,这个bug已经修复。 – jon3laze 2013-08-02 17:50:48