How do you add CSS with Javascript?

前端 未结 14 2754
旧时难觅i
旧时难觅i 2020-11-22 17:18

How do you add CSS rules (eg strong { color: red }) by use of Javascript?

14条回答
  •  深忆病人
    2020-11-22 17:44

    You can add classes or style attributes on an element by element basis.

    For example:

    text
    

    Where you could do this.style.background, this.style.font-size, etc. You can also apply a style using this same method ala

    this.className='classname';
    

    If you want to do this in a javascript function, you can use getElementByID rather than 'this'.

提交回复
热议问题