Modify CSS classes using Javascript

前端 未结 3 826
野性不改
野性不改 2020-12-30 00:23

I was wondering if there is an easy way to change the CSS classes in JavaScript. I have gone through all other similar questions here and I couldn\'t find an straight-forwar

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 00:36

    If your rules start incrementing you could extract your css to a new class and switch classes:

    CSS:

    .container-1{
      /* A set of rules */
    }
    .container-2{
      /* A set of rules */
    }
    

    JavaScript:

    element.className = element.className.replace(/container-1/, 'container-2')
    

提交回复
热议问题