Learn how to change the class name of an element with JavaScript.
Change the class of a <div> element from "mystyle" to "newone":
<div id="myDIV" class="mystyle">
This is a DIV element.
</div>
<script>
function myFunction() {
const element = document.getElementById("myDIV"); // Get the DIV element
element.classList.remove("mystyle"); // Remove mystyle class from DIV
element.classList.add("newone"); // Add newone class to DIV
}
</script>
Try it Yourself »
Tip: Also see How To Toggle A Class.
Tip: Learn more about the classList property in our JavaScript Reference.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!