Replace a CSS class with another:
const list = element.classList;
list.replace("myStyle", "newStyle");
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 »
Toggle "myStyle" on and off:
const list = element.classList;
list.toggle("myStyle");
Try it Yourself »
The replace()
method replaces a token in a DOMTokenList.
domtokenlist.replace(old, new)
Parameter | Description |
old | Required. The token to replace. |
new | Required. The token to replace with. |
Type | Description |
Boolean | true if the token was replaced, otherwise false . |
domtokenlist.replace()
is an ECMAScript7 (ES7) feature.
ES7 (JavaScript 2016) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
domtokenlist.replace()
is not supported in internet Explorer or Edge 17 (or earlier).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!