使用jQuery按顺序添加和删除类 [英] add and remove classes in order with jQuery

查看:78
本文介绍了使用jQuery按顺序添加和删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图说,如果我的div没有活动的类添加它。如果它确实激活了类,则将其删除。

Im trying to say that, if my div doesnt have the class active add it. And if it does have the class active, remove it.

如下所示,只有我的代码添加了类,然后继续查询并在最后删除它,最佳解决方案是什么,2个单独点击功能?

Ive the following, only my code adds the class, then continues the query and removes it at the end, what would the best solution be, 2 seperat click funcitons?

$('.work-showcase').click(function(){
    if ( !$(this).hasClass('active') ){
        $(this).addClass('active');
    } else {
        $(this).removeClass('active');
    };
});


推荐答案

使用 toggleClass 方法:

$(".work-showcase").click(function() {
    $(this).toggleClass("active");
})

这篇关于使用jQuery按顺序添加和删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆