Setting CSS pseudo-class rules from JavaScript

后端 未结 13 1684
爱一瞬间的悲伤
爱一瞬间的悲伤 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:39

    As already stated this is not something that browsers support.

    If you aren't coming up with the styles dynamically (i.e. pulling them out of a database or something) you should be able to work around this by adding a class to the body of the page.

    The css would look something like:

    a:hover { background: red; }
    .theme1 a:hover { background: blue; }
    

    And the javascript to change this would be something like:

    // Look up some good add/remove className code if you want to do this
    // This is really simplified
    
    document.body.className += " theme1";  
    

提交回复
热议问题