最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery nested accordion - disable auto open - Stack Overflow

programmeradmin1浏览0评论

I am trying to make it so that the accordion (#subAccordion1) within the parent (#accordion) is closed by default (note: there is only one subAccordion).

I am having some trouble doing this..

Here is my jQuery that I have tried:

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

And my 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>

The 2 outer accordions (1 and 2) are working as intended just the inner one (1.1) is automatically expanded!

Thanks for any help

I am trying to make it so that the accordion (#subAccordion1) within the parent (#accordion) is closed by default (note: there is only one subAccordion).

I am having some trouble doing this..

Here is my jQuery that I have tried:

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

And my 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>

The 2 outer accordions (1 and 2) are working as intended just the inner one (1.1) is automatically expanded!

Thanks for any help

Share Improve this question edited Apr 3, 2013 at 20:13 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Apr 3, 2013 at 20:11 New_programmerNew_programmer 2853 gold badges7 silver badges16 bronze badges 2
  • Do you have jsfiddle link? – Khanh Tran Commented Apr 3, 2013 at 20:15
  • may we assume your missing </div> is a typo? – Mark Schultheiss Commented Apr 3, 2013 at 20:23
Add a ment  | 

2 Answers 2

Reset to default 5

Fix your HTML first (missing closing 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"
   });
});

Try

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

instead of

$("#accordion, #subAccordion1").accordion(function() {
发布评论

评论列表(0)

  1. 暂无评论