HTML DOM classList property
Example
is
document.getElementById(
"myDIV"
).classList.add(
"mystyle"
);
Try it »
Definition and Usage
The classList property returns the class names of an element as a DOMTokenList object.
This property is used to add, remove and toggle CSS classes on an element.
The classList property is read-only, but you can modify it using the add() and remove() methods.
Browser support
The numbers in the table indicate the version number of the first browser that supports this property.
| property | |||||
|---|---|---|---|---|---|
| classList | 8.0 | 10.0 | 3.6 | 5.1 | 11.5 |
Syntax
element
.classList
Properties
| property | Description |
|---|---|
| length | Return the number of classes in the class list |
该属性是只读的 |
Method
| Method | Description |
|---|---|
| add(class1, class2, ...) | Add one or more class names to an element. |
如果指定的类名已存在,则不会添加 |
| contains(class) | Returns a Boolean indicating whether the specified class name exists.
Possible values:
- true - The element already contains this class name
- false - The class name does not exist in the element
如果索引值在区间范围外则返回 *null* |
| remove(class1, class2, ...) | Removes one or more class names from the element.
**注意:** 移除不存在的类名,不会报错。 |
| toggle(*class, *true|false) | Toggles a class name in the element.
第一个参数为要在元素中移除的类名,并返回 false。
如果该类名不存在则会在元素中添加类名,并返回 true。
The second is an optional parameter, a boolean value used to set whether the element should forcibly add or remove a class, regardless of whether that class name exists. For example:
移除一个 class: *element*.classList.toggle("classToRemove", false);
添加一个 class: *element*.classList.toggle("classToAdd", true);
**Note: ** Internet Explorer or Opera 12 and earlier versions do not support the second parameter. |
Technical description
| Return Value: | A DOMTokenList containing the list of class names of the element |
|---|
More examples
Example
is
document.getElementById(
"myDIV"
).classList.add(
"mystyle"
,
"anotherClass"
,
"thirdClass"
);
Try it »
Example
is
document.getElementById(
"myDIV"
).classList.remove(
"mystyle"
);
Try it »
Example
is
document.getElementById(
"myDIV"
).classList.remove(
"mystyle"
,
"anotherClass"
,
"thirdClass"
);
Try it »
Example
is
document.getElementById(
"myDIV"
).classList.toggle(
"newClassName"
);
Try it »
Example
Obtain the
class=
"mystyle anotherClass thirdClass"
I am a DIV element
var
x = document.getElementById(
"myDIV"
).classList;
x The output result is:
mystyle anotherClass thirdClass
Try it »
Example
Check
var
x = document.getElementById(
"myDIV"
).classList.length;
x The output result is:
3
Try it »
Example
Obtain the
var
x = document.getElementById(
"myDIV"
).classList.item(
0
);
x The output result is:
mystyle
Try it »
Example
Check whether the element contains the "mystyle" class:
var
x = document.getElementById(
"myDIV"
).classList.contains(
"mystyle"
);
x The output result is:
true
Try it »
Example
Check whether the element contains the "mystyle" class, and if so, remove another class name:
var
x = document.getElementById(
"myDIV"
);
if
(x.classList.contains(
"mystyle"
)) {
x.classList.remove(
"anotherClass"
);
}
else
{
alert(
"Could not find it."
);
}
Try it »
Related Articles
CSS Tutorial: CSS selector
CSS Reference Manual: CSS .class Selector
HTML DOM Reference Manual: HTML DOM className property
HTML DOM Reference Manual: HTML DOM getElementsByClassName() method
HTML DOM Reference Manual: HTML DOM Style Object
Element object