为 <div> 元素添加 class:

document.getElementById(“myDIV”).classList.add(“mystyle”);

为 <div> 元素添加多个类:

document.getElementById(“myDIV”).classList.add(“mystyle”, “anotherClass”, “thirdClass”);

为 <div> 元素移除一个类:

document.getElementById(“myDIV”).classList.remove(“mystyle”);

为 <div> 元素移除多个类:

document.getElementById(“myDIV”).classList.remove(“mystyle”, “anotherClass”, “thirdClass”);

检查是否含有某个CSS类

myDiv.classList.contains(‘myCssClass’); //return true or false

原地址:https://blog.csdn.net/stone10086/article/details/78390353

05-11 15:23