「jQuery」toggleClass()でボタンを押した時にクラスを付与する

説明
ボタンを押した時にクラスを付与する場合は toggleClass() を使用します。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$(function(){
$('.button').click(function() {
$(this).next().slideToggle();
$(this).toggleClass('active');
}).next().hide();
});
$(function(){ $('.button').click(function() { $(this).next().slideToggle(); $(this).toggleClass('active'); }).next().hide(); });
$(function(){
   $('.button').click(function() {
     $(this).next().slideToggle();
     $(this).toggleClass('active');
  }).next().hide();
});

説明
クラス名activeを必要なクラスに変更可能です

jQuery

Posted by arkgame