classList
API提供了原生的方式来添加,删除,切换,或检查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)
//根据表达式的结果添加或删除“someclass”。IE11或FF26或更低版本不支持。
element.classList.contains(“someclass”)
//检查元素中是否存在类
|