classList.add() 添加样式
classList.remove() 移除样式
Code:
<style>.current{color: red;}</style><body><p id="app">hello world</p><button id="btn">移除class</button><script>var app =document.getElementById("app");var btn =document.getElementById("btn");app.onclick = function(){this.classList.add("current")}btn.onclick = function(){app.classList.remove("current")}</script></body>
classList.toggle() 切换 样式添加/样式移除
Code:
<style>.current{color: red;}</style><body><p id="app">hello world</p><script>var app = document.getElementById("app");app.onclick =function(){this.classList.toggle("current")}</script></body>
classList.contains() 判断是否包含某个样式 返回值为 true / false
eg:
<style>.current{color: red;}</style><body><p id="app">hello world</p><script>var app = document.getElementById("app");app.onclick =function(){this.classList.toggle("current")console.log(this.classList.contains("current"))}</script></body>

