JavaScript (jQuery?): Toggle multiple classes with one click

后端 未结 2 975
天涯浪人
天涯浪人 2021-01-27 02:10

When the user clicks div.togglethis, I need two things to happen:

  1. Add an \"active\" class to div.togglethis

  2. Add a \"show\" class to an outside

2条回答
  •  半阙折子戏
    2021-01-27 02:50

    You cannot remove togglethis entirely otherwise the click won't work next time. Also, I'm not removing the class on second div for the same reason, rather I'm calling show/hide as I think that is what you intend to do.

    Click me to add "active"
    $('div.toggle').click(function(){ if($(this).hasClass('togglethis')) { $(this).removeClass('togglethis').addClass('active'); $('div.showthis').hide(); } else { $(this).removeClass(active).addClass('togglethis'); $('div.showthis').show(); } });

提交回复
热议问题