JS基础篇--HTML DOM classList 属性
页面DOM里的每个节点上都有一个classList对象,程序员可以使用里面的方法新增、删除、修改节点上的CSS类。使用classList,程序员还可以用它来判断某个节点是否被赋予了某个CSS类。 添加类(add) document.getElementById("myDIV").classList.add("mystyle"); 为 <div> 元素添加多个类: document.getElementById("myDIV").classList.add("mystyle", "anotherClass", "thirdClass"); 移除类(remove) 使用remove方法,你可以删除单个CSS类: document.getElementById("myDIV").classList.remove("mystyle"); 移除多个类: document.getElementById("myDIV").classList.remove("mystyle", "anotherClass", "thirdClass"); 切换类(toggle) 这个方法的作用就是,当myDiv元素上没有这个CSS类时,它就新增这个CSS类;如果myDiv元素已经有了这个CSS类,它就是删除它。就是反转操作。 document.getElementById("myDIV").classList