Pat*_*son 0 jquery twitter-bootstrap
我想在点击img元素时切换"active"类.但是当点击具有"活动"类的元素时,我希望将其删除.
这是我目前正在使用的JS:
$('.employee_mugshot').click(function() {
$(".employee_mugshot").removeClass("active");
$(this).addClass('active');
});
Run Code Online (Sandbox Code Playgroud)
这是我在JSFiddle中的完整示例:http://jsfiddle.net/patrickbeeson/6ec2K/
那就是toggleClass()
,只要确保你没有从当前单击的元素中删除该类,因为这个类总是会被添加
$('.employee_mugshot').click(function() {
$(".employee_mugshot").not(this).removeClass("active");
$(this).toggleClass('active');
});
Run Code Online (Sandbox Code Playgroud)