下面是我已經實現的手風琴,它看起來不錯,現在我試圖將第一個手風琴指定爲默認打開,但我無法做到。我認爲我必須用jquery完成,但我在Js和所有人都是noob。欣賞您的快速解決方案。如何通過dfault激活第一個手風琴
$(document).ready(function() {
//toggle the component with class accordion_body
$(".accordion_head").click(function() {
if ($('.accordion_body').is(':visible')) {
$(".accordion_body").slideUp(300);
$(".plusminus").text('+');
}
if ($(this).next(".accordion_body").is(':visible')) {
$(this).next(".accordion_body").slideUp(300);
$(this).children(".plusminus").text('+');
} else {
$(this).next(".accordion_body").slideDown(300);
$(this).children(".plusminus").text('-');
}
});
});
.accordion_container {
width: 100%;
}
.accordion_head {
background-color:#e74c3c;
color: white;
cursor: pointer;
font-family: 'Abel';
font-size: 20px;
margin: 0 0 1px 0;
padding: 7px 11px;
padding-left:35px;
font-weight: bold;
}
.accordion_body {
font-size: 20px!important;
background: transparent;
color:#666;
padding: 7px 11px;
padding-left:35px;
padding-right:35px;
}
.accordion_body p {
padding: 18px 5px;
margin: 0px;
}
.plusminus {
font-size: 20px!important;
float:right;
}
.accordion_container h5{
font-family: 'Abel';
text-transform: uppercase;
text-align: left;
color: #666;
text-shadow: 1px 1px #000;
font-weight: 100;
font-size: 24px;
line-height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="accordion_container">
<div class="accordion_head"><span class="glyphicon glyphicon-heart"></span>Accordion #01<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div><hr>
<div class="accordion_head"><span class="glyphicon glyphicon-heart"></span>Accordion #02<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div><hr>
<div class="accordion_head"><span class="glyphicon glyphicon-heart"></span>Accordion #03<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
只需添加一個'風格= 「顯示:塊;」'向第一'accordion_body'元件。而對於所有其他人只是做'style =「display:none;」' –
這一切似乎很容易通過使用Bootstrap實現。是否有任何具體的原因,你不用Bootstrap這樣做? –