js给节点添加或删除类名

时间:2018-07-24 17:56:02   收藏:0   阅读:1585

为 <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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!