一、add() — 增加class
<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")
}
</script>
二、remove() — 移除class
btn.onclick = function(){
app.classList.remove("current")
}
三、toggle():将某个 class 移入或移出当前元素
<!--
toggle toggleClass();
contains hasClass() 判断是否包含某个class
-->
<p id="app">hello world</p>
<script>
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")
}
</script>