问题
I'm using jqueryui's accordion control. I'm required to set up a navigation that involves accordions within accordions. It looks like it builds all the accordions okay. I expand the first one and see the second one. When I expand the second one, I see it expand, but then its parent accordion rolls up and hides the child accordion I just expanded.
This codepen shows my problem: http://codepen.io/chodenode/pen/Evnsz
Many thanks for any help:
My HTML:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css">
<body>
<ul id="navigation">
<li class="parent-li">
<span class="topic"><a href="">Cardiac</a></span>
<span class="header"></span>
<ul>
<li class="parent-li">
<span class="topic">Coronary Atherosclerosis</span>
<span class="header"></span>
<ul>
<li>
<a href="">Clinical Outcome Profile</a>
<span class="header"></span>
<ul>
<li>Coronary Atherosclerosis Per 1000 ACA</li>
<li>Coronary Atherosclerosis Comorbidity</li>
<li>Coronary Atherosclerosis % with PTCA</li>
<li>Coronary Atherosclerosis Mortality Rate</li>
</ul>
</li>
<li>
<a class="parent" href="">MS-DRG Care Management Profile</a>
<span class="header"></span>
<ul>
<li>MS-DRG 286 Circulatory disorders except AMI w card cath w MCC</li>
<li>MS-DRG 302 Atherosclerosis w MCC</li>
<li>MS-DRG 303 Atherosclerosis w/o MCC</li>
<li>MS-DRG 304 Hypertension w MCC</li>
</ul>
</li>
</ul>
</li>
<li class="parent-li">
<span class="topic">Chest Pain</span>
</li>
<li class="parent-li">
<span class="topic">Angina</span>
</li>
</ul>
</li>
<li class="parent-li">
<span class="topic">Vascular</span>
<span class="header"></span>
<ul></ul>
</li>
<li class="parent-li">
<span class="topic">Respiratory</span>
<span class="header"></span>
<ul></ul>
</li>
</ul>
</body>
My code:
$(document).ready(function() {
$('#navigation').accordion({
header: '.header',
event: 'click',
collapsible: true,
heightStyle: 'content',
active: false,
fillSpace: true,
});
$('a').click(function(event) {
event.preventDefault();
});
});
回答1:
Is https://stackoverflow.com/a/14616985/1847695 any help.
It looks like the each nested accordian is within a <div>
tag, then the javascript becomes:
$("div.accordian").accordion({ <options> });
This was about the only option I haven't yet tried on your codepen sample.
回答2:
You need to have a seperate id
and header
for the sub-accordion. by using the same id
and header
jQuery is collapsing everything at the same time.
The following should get you on the right track... I just copied your original accordion declaration and updated it with a sub_
prefix
$(document).ready(function() {
$('#navigation').accordion({
header: '.header',
event: 'click',
collapsible: true,
heightStyle: 'content',
active: false,
fillSpace: true,
});
$('#sub_navigation').accordion({
header: '.sub_header',
event: 'click',
collapsible: true,
heightStyle: 'content',
active: false,
fillSpace: true,
});
Then update your html to reflect the script changes:
<body>
<ul id="navigation">
<li class="parent-li">
<span class="topic"><a href="">Cardiac</a></span>
<span class="header"></span>
<ul>
<li id="sub_navigation" class="parent-li">
<span class="topic">Coronary Atherosclerosis</span>
<span class="sub_header"></span>
来源:https://stackoverflow.com/questions/15094108/accordion-within-accordion-collapses-parent-accordion