Javascript 如何确定引导程序崩溃是打开还是关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33092386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if a bootstrap collapse is opening or closing?
提问by user1186050
If I have a bootstrap collapse how can I determine from a click event wether the collapse is opening or closing?
如果我有 bootstrap 折叠,我如何从点击事件中确定折叠是打开还是关闭?
Here is my click event or maybe there is a better way then to use a click event?
这是我的点击事件,或者也许有更好的方法来使用点击事件?
$(document).on("click", "a.register-student-link", function() {
// do some stuff to check if opening or closing
}
<div>
<a [email protected] class="register-student-link" data-toggle="collapse" href=@spaceIdWith aria-expanded="false" aria-controls="collapseExample">
Register Student
</a>
</div>
回答by user1186050
Bootstrap uses the aria-expanded attribute to show true or false if the region is collapsed or not.
Bootstrap 使用 aria-expanded 属性来显示区域是否折叠的真或假。
var isExpanded = $(collapsableRegion).attr("aria-expanded");
回答by Josiah Krutz
Try:
尝试:
$('#collapseDiv').on('shown.bs.collapse', function () {
console.log("Opened")
});
$('#collapseDiv').on('hidden.bs.collapse', function () {
console.log("Closed")
});
<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"/>
<div>
<a [email protected] class="register-student-link" data-toggle="collapse" href="#collapseDiv" aria-expanded="false" aria-controls="collapseExample">Register Student</a>
</div>
<div class="collapse" id="collapseDiv">This is the collapsible content!</div>
(based on https://stackoverflow.com/a/18147817/2033574(Kevin mentioned) and http://www.bootply.com/73101)
(基于https://stackoverflow.com/a/18147817/2033574(提到凯文)和http://www.bootply.com/73101)
回答by adam3039
I needed a way to determine if the element was collapsed BEFORE actually collapsing. Whereas the event listeners only trigger afterwards.
我需要一种方法来确定元素在实际折叠之前是否已折叠。而事件侦听器仅在之后触发。
//Will return true if uncollapsed
$('#collapseDiv').hasClass('in');
//Will return true if in the process of collapsing
$('#collapseDiv').hasClass('collapsing');
//Will return true if collapsed
$('#collapseDiv').hasClass('collapse');
回答by Kevin Friedheim
You can watch the event hidden.bs.collapse
您可以观看活动 hidden.bs.collapse
see fiddle: http://jsfiddle.net/kyeuvx1d/
见小提琴:http: //jsfiddle.net/kyeuvx1d/
回答by sam007
if(!$("#id").hasClass('show')){
alert("Uncollapsed");
}
else {
alert("Collapsed");
}
for Bootstrap 4
引导程序 4