[英]JQuery - How can I determine if a specific class has been Toggled?
<div class="tab1"><div>
<div class="tab1 active"></div>
Given the code above how can I tell using jQuery that the tab1 class has been changed or 'made active' 给定上面的代码,我如何使用jQuery告知tab1类已更改或“变为活动”
One solution I think there is a better version of is adding a id to the tab1 div and checking if the class has 'active' appended on it. 我认为有一个更好的解决方案是在tab1 div中添加一个id,并检查该类是否附加了“ active”。
[psuedocode below] [下面的伪代码]
$('#tab1').click(function() {
var myClass = $(this).attr("class");
if(myClass.stringAt(6-12) == active){
// do something
}
});
You can use hasClass()
to check whether it has the class
active
. 您可以使用
hasClass()
来检查其是否具有active
class
。 Also note that in your code tab1
is a class and not ID so it should be 还要注意,在您的代码中
tab1
是一个类而不是ID,因此它应该是
$('.tab1').click(function() {//Note that tab1 is a class in your code
if($(this).hasClass('active')){// Check if the div has the class active or not
//Do your operation
})
})
I think you only need to use :visible and :hidden via the is function which can be seen in use below, please note that I wrote it as a unit test so you can, by yourself, test different scenarios and learn from them. 我认为您只需要通过is函数使用:visible和:hidden(可以在下面的用法中看到),请注意,我将其编写为单元测试,因此您可以自己测试不同的场景并从中学习。 Hope it helps!
希望能帮助到你!
HTML 的HTML
<div class="tab1"></div>
<div class="tab1 active"></div>
jQuery 2.1.3 jQuery 2.1.3
jQuery(document).ready(function(){
if(jQuery(".tab1").is(":visible")){
alert("tab1 is visible");
}
else{
alert("nothing is :visible");
}
if(jQuery(".tab1.active").is(":visible")){
alert("tab1 active is visible");
}
else{
alert("nothing is :visible");
}
});
Take a look here for a 'visual' example: jsFiddle 看看这里的“视觉”示例: jsFiddle
jQuery reference: Determining the state of a toggled element jQuery参考: 确定切换元素的状态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.