单击按钮或“插入符号”图标后,它将展开并显示内容。同时,按钮中的图标将更改为“减号”图标,但似乎不起作用。这是代码。
$('#demo').on('shown.bs.collapse', function() {
var getID = $(this).attr("id");
getID = $("#toggle-" + getID);
$(getID).toggleClass("fa-caret-down fa-minus")
});
$('#demo').on('hidden.bs.collapse', function() {
var getID = $(this).attr("id");
getID = $("#toggle-" + getID);
$(getID).toggleClass("fa-minus fa-caret-down");
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="../../css/register-dev.css">
<link rel="stylesheet" href="../../css/activity-registration.css">
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<button class="btn btn-light btn-lg" type="button" data-toggle="collapse" data-target="#demo">Detail
<span><i id="toggle-demo" class="fa fa-caret-down" aria-hidden="true"></i></span>
</button>
</div>
</div>
</div>
<div id="demo" class="collapse in">Some dummy text in here.</div>
答案 0 :(得分:0)
由于您的button
不是div
的父级,因此无法使用parent()
方法,并且您在同一行删除并添加相同的课程时出错了,您也可以使用toggleClass()
。正确的方法是下一步:
$('#demo').on('shown.bs.collapse', function () {
console.log("Opened")
$('button[data-target="#demo"] > span > i.fa').toggleClass("fa-caret-down fa-minus")
});
$('#demo').on('hidden.bs.collapse', function () {
console.log("Closed")
$('button[data-target="#demo"] > span > i.fa').toggleClass("fa-minus fa-caret-down")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<button class="btn btn-light btn-lg" type="button" data-toggle="collapse" data-target="#demo">
Detail
<span>
<i class="fa fa-caret-down" aria-hidden="true"></i>
</span>
</button>
<div id="demo" class="collapse">Some dummy text in here.</div>
注意:如果您愿意,也可以将下一个选择器与
find()
方法结合使用:
$('button[data-target="#demo"]').find("i.fa").toggleClass("fa-caret-down fa-minus")
答案 1 :(得分:0)
更新了代码段
我更愿意使用IDs
。提供ID i
标记。我知道有很多方法可以做。但是使用下面的解决方案。你会得到很好的表现。因为它会将id定位为不从DOM扫描元素
$('.collapse').on('shown.bs.collapse', function() {
var getID = $(this).attr("id");
getID = $("#toggle-" + getID);
$(getID).toggleClass("fa-caret-down fa-minus")
}).on('hidden.bs.collapse', function() {
var getID = $(this).attr("id");
getID = $("#toggle-" + getID);
$(getID).toggleClass("fa-minus fa-caret-down");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
<div class="row">
<div class="col">
<button class="btn btn-light btn-lg" type="button" data-toggle="collapse" data-target="#demo">Detail
<span><i id="toggle-demo" class="fa fa-caret-down" aria-hidden="true"></i></span>
</button>
</div>
</div>
</div>
<div id="demo" class="col collapse in">Some dummy text in here.</div>
<br>
<div class="col">
<button class="btn btn-light btn-lg" type="button" data-toggle="collapse" data-target="#demo2">Detail
<span><i id="toggle-demo2" class="fa fa-caret-down" aria-hidden="true"></i></span>
</button>
</div>
<div id="demo2" class="col collapse in">Some dummy text in here.</div>
&#13;