toggle toggleClass();
    contains hasClass() 判断是否包含某个class

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. <style>
    8. .current{
    9. color:red;
    10. }
    11. </style>
    12. </head>
    13. <body>
    14. <!--
    15. toggle toggleClass();
    16. contains hasClass() 判断是否包含某个class
    17. -->
    18. <p id="app">hello world</p>
    19. <script>
    20. var app = document.getElementById("app");
    21. app.onclick = function(){
    22. // if(this.classList.contains("current")){
    23. // this.classList.remove("current")
    24. // }else{
    25. // this.classList.add("current")
    26. // }
    27. this.classList.toggle("current")
    28. }
    29. </script>
    30. </body>
    31. </html>