Setting CSS pseudo-class rules from JavaScript

后端 未结 13 1711
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:01

I\'m looking for a way to change the CSS rules for pseudo-class selectors (such as :link, :hover, etc.) from JavaScript.

So an analogue of the CSS code: a:hove

13条回答
  •  梦如初夏
    2020-11-22 01:59

    My trick is using an attribute selector. Attributes are easier to set up by javascript.

    css

    .class{ /*normal css... */}
    .class[special]:after{ content: 'what you want'}
    

    javascript

      function setSpecial(id){ document.getElementById(id).setAttribute('special', '1'); }
    

    html

     ...  
    

提交回复
热议问题