<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>test</title> </head> <style> .s1 { border: 1px solid red; } .s2 { font-size: 100px; } .s3 { background-color: rgb(21, 202, 36); } </style> <body> <div class="s1" id="div1"> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx </div> <script> let div = document.getElementById("div1"); //这样会把div1 原来的s1样式覆盖,只有s2 div.setAttribute("class", "s2"); //这样是在原来的基础上追加 s2和s3 最后会同时有test1,test2,test3样式 div.classList.add("s2", "s3"); //移除test1样式 div.classList.remove("s1"); //同时移除多个样式s1,s2 div.classList.remove("s1", "s2"); //判断是否包含s1样式 div.classList.contains("s1"); </script> </body> </html>