2012-11-07 74 views
1

我在想如何打开特定的手风琴(实际上是一个切换键)。打开特定的手风琴

这里是我的jQuery

$(document).ready(function() 
    { 
     //Add Inactive Class To All Accordion Headers 
     $('.accordion-header').toggleClass('inactive-header'); 

     //Open The First Accordion Section When Page Loads 
     $('.accordion-header').first().toggleClass('active-header').toggleClass('inactive-header'); 
     $('.accordion-content').first().slideDown().toggleClass('open-content'); 

     // The Accordion Effect 
     $('.accordion-header').click(function() { 
      if($(this).is('.inactive-header')) { 
       $('.active-header').toggleClass('active-header').toggleClass('inactive-header').next().slideToggle().toggleClass('open-content'); 
       $(this).toggleClass('active-header').toggleClass('inactive-header'); 
       $(this).next().slideToggle().toggleClass('open-content'); 
      } 

      else { 
       $(this).toggleClass('active-header').toggleClass('inactive-header'); 
       $(this).next().slideToggle().toggleClass('open-content'); 
      } 
     }); 

     return false; 
    });​ 

现在会发生什么事是,在页面加载的第一个项目打开。我怎样才能将它设置到第二项打开的位置?

我这里有一个小提琴:http://jsfiddle.net/bbyrdhouse/LjDBa/

在此先感谢。

回答

0

自动打开第二手风琴,改变第一线这样:

$('.accordion-header').eq(1).toggleClass('active-header').toggleClass('inactive-header'); 
$('.accordion-content').eq(1).slideDown().toggleClass('open-content'); 

eq(1)将在匹配元素定位第二项(索引是从零开始)。如果您想打开最后一个手风琴,请使用last()而不是eq(1)

查看更新的提琴:http://jsfiddle.net/adrianonantua/LjDBa/1/