「jQuery」removeClassメソッドで複数のclass属性をまとめて削除する

環境
jQuery 3.6.0
Google Chrome 103.0.5060.114

構文
$(“div#ID名").removeClass(“クラス1 クラス2");
removeClassメソッドは複数のclass属性をまとめて削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<style>
.cft {
font-size: 15px;
}
.info {
background-color: skyblue;
}
</style>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<body>
<div class="cft">Tokyo</div>
<div id="city" class="cft info">Oosaka</div>
<button>削除</button>
<script>
$("button").click(function(){
$("div#city").removeClass("cft info");
});
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <style> .cft { font-size: 15px; } .info { background-color: skyblue; } </style> </head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <body> <div class="cft">Tokyo</div> <div id="city" class="cft info">Oosaka</div> <button>削除</button> <script> $("button").click(function(){ $("div#city").removeClass("cft info"); }); </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
<style>
  .cft {
    font-size: 15px;
  }
  .info {
     background-color: skyblue;
  }
</style>
</head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<body>
<div class="cft">Tokyo</div>
<div id="city" class="cft info">Oosaka</div>
<button>削除</button>

<script>
  $("button").click(function(){
      $("div#city").removeClass("cft info");
  });
</script>
</body>
</html>

実行結果
「削除」ボタンをクリックするとid属性がcityのdiv要素に対しremoveClassメソッドを利用してclass属性の「cft」と「info」を削除します。

jQuery

Posted by arkgame