Dùng classList.remove() để xóa một class khỏi element.
Ví dụ
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> .newStyle { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; width: 100%; padding: 25px; background-color: rgb(147, 80, 255); color: white; font-size: 25px; box-sizing: border-box; text-align: center; } </style> </head> <body> <h1>Xóa className với JavaScript</h1> <button class="btn">Xóa Class</button> <h2>Nhấn nút để xóa class khỏi div dưới</h2> <div class="newStyle" id="sampleDiv"> Đây là phần tử DIV. </div> <script> document.querySelector(".btn").addEventListener("click", addClassName); function addClassName() { var element = document.getElementById("sampleDiv"); element.classList.remove("newStyle"); } </script> </body> </html>
Kết quả
Div ban đầu có style (nền tím), sau khi nhấn nút class bị xóa, div trở về mặc định.