How do I add a class to a given element?

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

I have an element that already has a class:

25条回答
  •  深忆病人
    2020-11-21 12:02

    document.getElementById('some_id').className+='  someclassname'
    

    OR:

    document.getElementById('some_id').classList.add('someclassname')
    

    First approach helped in adding the class when second approach didn't work.
    Don't forget to keep a space in front of the ' someclassname' in the first approach.

    For removal you can use:

    document.getElementById('some_id').classList.remove('someclassname')
    

提交回复
热议问题