简体   繁体   中英

jQuery remove class after second click

I have my own drop down navigation working, so when a user clicks on one of the links a page overlay will appear. I just need when they click again the page overlay removes.

Here is my code to add the overlay

$('#nav li a').on('click', function(){
    $('#page-overlay').addClass('active').siblings().removeClass('active');
});

And a working DEMO is here - http://dsm.fishtankcreative.co.uk/

I just need help for when a user clicks off the navigation the page overlay class disappear.

Thanks in advanced.

Use toggleClass()

$('#nav li a').on('click', function(){
    $('#page-overlay').toggleClass('active').siblings().removeClass('active');
});

Note: I don't think there is a need to use .siblings().removeClass('active') , as you are not adding the active class to any other elements

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