Remove/add class on click of a button

前端 未结 2 2016
醉酒成梦
醉酒成梦 2021-01-22 07:12

I\'d like to be able to remove one class from a div and add another upon the click of a button. But I can\'t get it to work.

相关标签:
2条回答
  • 2021-01-22 07:45

    Try $(selector).toggleClass(class):

    $(document).ready(function(){ 
        $(".shownavbutton").click(function() { 
            $(".hiddennav").toggleClass("displaynone").toggleClass("displayblock");
        });
    });
    

    Optionally, you could use the CSS method as well (assuming that this is all you're accomplishing via your displaynone and displayblock classes):

    $(".hiddennav").toggle(function() {
        $(this).css('display','none');
    }, function() {
        $(this).css('display','block');
    });
    
    0 讨论(0)
  • 2021-01-22 07:51

    Try this

     $(".hiddennav").toggleClass("displaynone displayblock");
    
    0 讨论(0)
提交回复
热议问题