JavaScript CSS how to add and remove multiple CSS classes to an element

前端 未结 14 1989
迷失自我
迷失自我 2020-12-07 21:46

How can assign multiple css classes to an html element through javascript without using any libraries?

14条回答
  •  天涯浪人
    2020-12-07 22:42

    var el = document.getElementsByClassName('myclass')
    
    el[0].classList.add('newclass');
    
    el[0].classList.remove('newclass');
    

    To find whether the class exists or not, use:

    el[0].classList.contains('newclass'); // this will return true or false 
    

    Browser support IE8+

提交回复
热议问题