Toggle "myStyle" on and off:
const list = element.classList;
list.toggle("mywStyle");
Try it Yourself »
Add a "myStyle" class to an element:
const list = element.classList;
list.add("myStyle");
Try it Yourself »
Remove the "myStyle" class from an element:
const list = element.classList;
list.remove("myStyle");
Try it Yourself »
More examples below.
The add()
method adds one (or more) tokens to a DOMTokenList.
domtokenlist.toggle(token)
Parameter | Description |
token | Required. The token to toggle. |
NONE |
Add multiple classes to the an element:
element.classList.add("myStyle", "anotherClass", "thirdClass");
Try it Yourself »
Remove multiple classes from an element:
element.classList.remove("myStyle", "anotherClass", "thirdClass");
Try it Yourself »
Toggle between classes to create a dropdown button:
document.getElementById("myBtn").onclick = function() {myFunction()};
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
Try it Yourself »
domtokenlist.toggle()
is a DOM Level 4 (2015) feature.
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
domtokenlist.toggle()
is not supported in Internet Explorer 11 (or earlier).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!