以下のコードを使用して、ボタンから離れた後も 1 つのボタンをアクティブなままにすることができます。
$('.btn').click(function(){
if($(this).hasClass('active')){
$(this).removeClass('active')
} else {
$(this).addClass('active')
}
});
しかし、ページ上の他のすべてのボタンも非アクティブにしたいと考えています。 次のことを試しましたが、すべてを非アクティブにする式は、クリック関数の外にある必要があります。 クリック関数内でループを実行してボタン全体を繰り返すにはどうすればよいですか?
$('.btn').click(function(){
//make all inactive-doesn't work
$( '.btn' ).each(function( ) {
if($(this).hasClass('active')){
$(this).removeClass('active')
}
});
if($(this).hasClass('active')){
$(this).removeClass('active')
} else {
$(this).addClass('active')
}
});
コメント