2010-07-20 107 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