admin管理员组文章数量:1405112
Relevant bootstrap.js code:
...
Dropdown.prototype = {
constructor: Dropdown
, toggle: function (e) {
var $this = $(this)
, $parent
, isActive
$parent = getParent($this)
isActive = $parent.hasClass('open')
....
Is there a way to check for an open dropdown using isActive in a oneliner jquery like:
if($('#dropdownElement').isActive)
I know I can do something like:
var parent = $('#aParentID');
if(parent.hasClass('open')) { ...
But I wonder if I'm able to do it that way, thanks.
Relevant bootstrap.js code:
...
Dropdown.prototype = {
constructor: Dropdown
, toggle: function (e) {
var $this = $(this)
, $parent
, isActive
$parent = getParent($this)
isActive = $parent.hasClass('open')
....
Is there a way to check for an open dropdown using isActive in a oneliner jquery like:
if($('#dropdownElement').isActive)
I know I can do something like:
var parent = $('#aParentID');
if(parent.hasClass('open')) { ...
But I wonder if I'm able to do it that way, thanks.
Share Improve this question asked Jul 10, 2013 at 2:36 Antonio MaxAntonio Max 8,8337 gold badges44 silver badges43 bronze badges 2- 2 you can write is as a plugin, if you want – Arun P Johny Commented Jul 10, 2013 at 2:37
-
2
There's
is
too:if ($('#parent').is('.open'))
– elclanrs Commented Jul 10, 2013 at 2:38
2 Answers
Reset to default 3If you need a one liner, then write it as a plugin
(function($){
$.fn.isActive = function(){
console.log(this)
return $(this.get(0)).hasClass('open')
}
})(jQuery)
Then
$('#dropdownElement').isActive()
Demo: Fiddle
The accepted solution did not work for me. I was using Bootstrap 4, maybe something has changed. I digged into dropdown.js
code and I found this:
const ClassName = {
...
SHOW : 'show',
...
}
...
toggle() {
...
const isActive = $(this._menu).hasClass(ClassName.SHOW)
...
}
So the solution for me was:
var isShown = document.getElementById('dropdownElement').classList.contains('show');
Using jQuery, that'd be:
var isShown = $("#dropdownElement").hasClass("show");
本文标签: javascriptDropdownCheck if open using quotisActivequot propertyStack Overflow
版权声明:本文标题:javascript - Dropdown - Check if open using "isActive" property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744277883a2598505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论