classList
API提供了原生的方式來(lái)添加,刪除,切換,或檢查CSS類存在的元素。它比解析容易得多
element.className
:
1
2
3
4
五
6
7
8
9
10 |
element.classList.add(
"someclass"
)
//adds a class to element
element.classList.add(
"class1"
,
"class2"
, etc)
//adds multiple CSS classes. Not supported in IE11 or FF26 or less.
element.classList.remove(
"someclass"
)
//removes a class from element
element.classList.remove(
"class1"
,
"class2"
, etc)
//removes multiple CSS classes. Not supported in IE11 or FF26 or less.
element.classList.toggle(
"someclass"
)
//toggles a class. If already exists removes and returns false. Otherwise adds class and returns true
element.classList.toggle(“someclass”,expression)
//根據(jù)表達(dá)式的結(jié)果添加或刪除“someclass”。IE11或FF26或更低版本不支持。
element.classList.contains(“someclass”)
//檢查元素中是否存在類
|