classList
classList 属性返回元素的类名,作为 DOMTokenList 对象。该属性用于在元素中添加,移除及切换 CSS 类
1 add -remove
js jqueryadd() addClass()remove() removeClass()
<p id="app">hello world</p><button id="btn">移除Class</button>var app = document.getElementById("app")var btn = document.getElementById("btn")app.onclick=function(){ this.classList.add("current")}btn.onclick = function(){ app.classList.remove("current")
2 toggle - contains
js jquerytoggle toggleClass()contains hasClass() 判断是否包含某个class
<p id="app">hello world</p>var app = document.getElementById("app")app.onclick = function(){ /* if(this.classList.contains("current")){ this.classList.remove("current") }else{ this.classList.add("current") } */ this.classList.toggle("current")}