试试这个
JsFiddle:http: //jsfiddle.net/ZGTJb/
CSS
.head {
background: #eee;
cursor: pointer;
}
.section .head, .section .section {
margin-left: 20px;
}
.section{
display:none;
}
脚本
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$('#accordion .head').click(function () {
var head = $(this);
// remove any active head
head.siblings('.head').removeClass('active');
var section = head.next('.section');
//remove .section to exclude from hide all
section.removeClass('section');
//hide sibling sections
head.siblings('.section').hide();
// set .section class back
section.addClass('section');
if( !section.css(':visible')) {
// set as active and show section
head.addClass('active');
section.fadeIn(500);
};
});
});
</script>
HTML
<div id="accordion">
<h3 class="head">
section 1</h3>
<div class="section">
<p>
section 1
</p>
<h3 class="head">
section 1.1</h3>
<div class="section">
<p>
section 1.1
</p>
</div>
<h3 class="head">
section 1.2</h3>
<div class="section">
<p>
section 1.2
</p>
<h3 class="head">
section 1.2.1</h3>
<div class="section">
<p>
section 1.2.1
</p>
</div>
<h3 class="head">
section 1.2.2</h3>
<div class="section">
<p>
section 1.2.2
</p>
</div>
</div>
</div>
<h3 class="head">
section 2</h3>
<div class="section">
<p>
section 2
</p>
</div>
</div>