How do I add a class to a given element?

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

I have an element that already has a class:

相关标签:
25条回答
  • 2020-11-21 12:19

    Sample with pure JS. In first example we get our element's id and add e.g. 2 classes.

    document.addEventListener('DOMContentLoaded', function() {
        document.getElementsById('tabGroup').className = "anyClass1 anyClass2";
    })
    

    In second example we get element's class name and add 1 more.

    document.addEventListener('DOMContentLoaded', function() {
        document.getElementsByClassName('tabGroup')[0].className = "tabGroup ready";
    })
    
    0 讨论(0)
提交回复
热议问题