简体   繁体   中英

add and remove classes in order with jQuery

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.

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');
    };
});

Use toggleClass method:

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

$("#YourID").removeClass('ClassName'); $("#YourID").addClass('ClassName');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM