JavaScriptのclassListについて
Last updated at Posted at 2023-07-28
classListとは
要素のクラスを追加、削除、参照、操作するプロパティ
getAttribute使うより断然簡単
メゾット |
内容 |
使い方 |
.add() |
classを追加 |
h1.classList.add('red'); |
.remove() |
classをはずす |
h1.classList.remove('red'); |
.toggle() |
クラスが含まれてたら削除、クラスが含まれてなかったら追加 |
h1.classList.toggle('red'); |
.contains() |
特定のclassが設定しているか確認 |
h1.classList.contains('red');→true |
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
What you can do with signing up