2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【jQuery】addClassに配列が使えるようになった

Posted at

jQuery3.3.1がリリースされていた(!)

既に1ヶ月以上前の話ですが、
クラスを操作するメソッドに配列が使えるようになったらしいので
試してみました。

jQuery 3.3.1
3.3.0のリリースでは問題が発生したので、3.3.1が推奨されています。

jQuery 3.3.0 blog

The .addClass(), .removeClass(), and .toggleClass() methods now accept an array of classes.

jQuery3.3.1 配列でクラス付与

See the Pen jquery3.3.1/addClass/array by kayukihashimoto (@kfunnytokyo) on CodePen.

ちゃんと2つのクラスが付与されています。
記述した順番に付与されているので、
blueに当てたcssが適応されていますね。

やったね!

jQuery3.2.1 配列でクラス付与

See the Pen jquery3.2.1/addClass/array by kayukihashimoto (@kfunnytokyo) on CodePen.

念のため、違うver.(3.2.1)でも試してみます。
もちろん、クラスは付与されません。

jQuery3.3.1 クラス付与

See the Pen jquery3.3.1/addClass by kayukihashimoto (@kfunnytokyo) on CodePen.

一度に複数のクラスを付与するなら、わざわざ配列にしなくても、
スペース区切りで指定してあげれば良いですよね。

jQuery3.2.1 変数に入れた配列でクラス付与

See the Pen jquery3.3.1/addClass/array/variable by kayukihashimoto (@kfunnytokyo) on CodePen.

例えば、別で管理している配列があれば、
変数でクラスの付与が出来ますね。

頭の隅に置いておこう〜。

おわり!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?