jQuery嵌套手风琴 - 禁用自动打开

时间:2013-04-03 20:11:22

标签: javascript jquery html accordion

我正在尝试这样做,以便父级(#subAccordion1)中的手风琴(#accordion)默认关闭(注意:只有一个subAccordion)。

我在做这件事时遇到了一些麻烦..

这是我尝试过的jQuery:

$(function() {
    $("#accordion, #subAccordion1").accordion(function() {
        autoHeight: false
        active: false
        collapsible: true
    });
});

我的HTML:

<div id = "accordion">
     <h1>Accordion1:</h1>
     <div>
         <p>Blah</p>

         <div id = "subAccordion1">
             <h1>Accordion1.1</h1>
             <div>
                 <p> BLAH BLAH BLAH</p>
             </div>
         </div>
      <h1>Accordion2:</h1>
      <div>
           <p>Blah</p>
      </div>
</div>

2个外部手风琴(1和2)按预期工作,内部手风琴(1.1)自动展开!

感谢您的帮助

2 个答案:

答案 0 :(得分:5)

首先修复你的HTML(缺少结束div):

<div id = "accordion">
     <h1>Accordion1:</h1>
     <div>
         <p>Blah</p>
         <div id = "subAccordion1">
             <h1>Accordion1.1</h1>
             <div>
                 <p> BLAH BLAH BLAH</p>
             </div>
         </div>
      </div>
      <h1>Accordion2:</h1>
      <div>
           <p>Blah</p>
      </div>
</div> 

的JavaScript

$(function() {
  // init parent accordion
  $("#accordion").accordion( { heightStyle: "content" });

  // init sub accordion via #id selector with options object
  $("#subAccordion1").accordion({
                autoHeight: false,
                active: false,
                collapsible: true,
                heightStyle: "content"
   });
});

答案 1 :(得分:3)

尝试

$("#accordion, #subAccordion1").accordion({

而不是

$("#accordion, #subAccordion1").accordion(function() {