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()中排除当前元素。我为你更新了答案 –