How do I add a class to a given element?

前端 未结 25 2450
清酒与你
清酒与你 2020-11-21 11:34

I have an element that already has a class:

25条回答
  •  旧时难觅i
    2020-11-21 12:01

    Another approach to add the class to element using pure JavaScript

    For adding class:

    document.getElementById("div1").classList.add("classToBeAdded");
    

    For removing class:

    document.getElementById("div1").classList.remove("classToBeRemoved");
    

提交回复
热议问题