一、add() — 增加class

  1. <p id="app">hello world</p>
  2. <button id="btn">移除class</button>
  3. <script>
  4. var app = document.getElementById("app");
  5. var btn = document.getElementById("btn");
  6. app.onclick = function(){
  7. this.classList.add("current")
  8. }
  9. </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>