1
這可能被問了好幾次,但我掙扎了一下。希望有人能幫忙。 HTML & jQuery下面,只想在div打開時將'.active'類添加到按鈕,並在關閉時刪除.active。jQuery顯示/隱藏 - 添加/刪除.active類
jQuery(function() {
jQuery('.expand').on('click', function(e) {
e.preventDefault();
var href = jQuery(this).attr("data-href");
jQuery("#" + href).slideToggle('slow', function() {
}).siblings('div.expanded').hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-lg-10">
<strong>Short Description</strong>
</div>
<div class="col-lg-2">
<a data-href="one" href="#" class="expand btn btn-primary btn-sm">more...</a>
</div>
</div>
<div id="one" style="display: none;" class="expanded row">
<strong>Long Description</strong>
</div>
<div class="row">
<div class="col-lg-10">
<strong>Short Description</strong>
</div>
<div class="col-lg-2">
<a data-href="two" href="#" class="expand btn btn-primary btn-sm">more...</a>
</div>
</div>
<div id="two" style="display: none;" class="expanded row">
<strong>Long Description</strong>
</div>
<div class="row">
<div class="col-lg-10">
<strong>Short Description</strong>
</div>
<div class="col-lg-2">
<a data-href="three" href="#" class="expand btn btn-primary btn-sm">more...</a>
</div>
</div>
<div id="three" style="display: none;" class="expanded row">
<strong>Long Description</strong>
</div>
完美的感謝羅裏 – devofash
還有一兩件事,如果我點擊相同的按鈕,然後再次關閉它的股利,但保持上的按鈕活動類。 – devofash
在這種情況下,您需要從removeClass()中排除當前元素。我爲你更新了答案 –