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

前端 未结 14 1979
迷失自我
迷失自我 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:18

    You can add and remove multiple classes in same way with different in-built methods:

    const myElem = document.getElementById('element-id');
    //add multiple classes
    myElem.classList.add('class-one', 'class-two', 'class-three');
    //remove multiple classes
    myElem.classList.remove('class-one', 'class-two', 'class-three');
    

提交回复
热议问题