简体   繁体   中英

Checking styles before changing?

I have a menu and I'm showing some content only if the user is on the home page. This condition runs every time any menu item is clicked. If the user clicks on the other menu items I do not want to display this content. I'm trying to prevent the JavaScript from having to go back and forth from the HTML. Is this excessive?

Example:

if(pageNumber === home) {
    $('#content').css({'display':'block'});
} else if($('#content').css('display') !== 'none') {
    $('#content').css({'display':'none'});
}

You can just use the toggle method:

$('#content').toggle(pageNumber === home);

http://api.jquery.com/toggle/

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