Setting CSS pseudo-class rules from JavaScript

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

    I threw together a small library for this since I do think there are valid use cases for manipulating stylesheets in JS. Reasons being:

    • Setting styles that must be calculated or retrieved - for example setting the user's preferred font-size from a cookie.
    • Setting behavioural (not aesthetic) styles, especially for UI widget/plugin developers. Tabs, carousels, etc, often require some basic CSS simply to function - shouldn't demand a stylesheet for the core function.
    • Better than inline styles since CSS rules apply to all current and future elements, and don't clutter the HTML when viewing in Firebug / Developer Tools.
    0 讨论(0)
  • 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";  
    
    0 讨论(0)
  • 2020-11-22 01:39

    If you use REACT , There is something called radium. It is so useful here :

    • Add handlers to props if interactive styles are specified, e.g. onMouseEnter for :hover, wrapping existing handlers if necessary

    • If any of the handlers are triggered, e.g. by hovering, Radium calls setState to update a Radium-specific field on the components state object

    • On re-render, resolve any interactive styles that apply, e.g. :hover, by looking up the element's key or ref in the Radium-specific state

    0 讨论(0)
  • 2020-11-22 01:42

    There is another alternative. Instead of manipulating the pseudo-classes directly, create real classes that model the same things, like a "hover" class or a "visited" class. Style the classes with the usual "." syntax and then you can use JavaScript to add or remove classes from an element when the appropriate event fires.

    0 讨论(0)
  • 2020-11-22 01:45

    You can't style a pseudo-class on a particular element alone, in the same way that you can't have a pseudo-class in an inline style="..." attribute (as there is no selector).

    You can do it by altering the stylesheet, for example by adding the rule:

    #elid:hover { background: red; }
    

    assuming each element you want to affect has a unique ID to allow it to be selected.

    In theory the document you want is http://www.w3.org/TR/DOM-Level-2-Style/Overview.html which means you can (given a pre-existing embedded or linked stylesheet) using syntax like:

    document.styleSheets[0].insertRule('#elid:hover { background-color: red; }', 0);
    document.styleSheets[0].cssRules[0].style.backgroundColor= 'red';
    

    IE, of course, requires its own syntax:

    document.styleSheets[0].addRule('#elid:hover', 'background-color: red', 0);
    document.styleSheets[0].rules[0].style.backgroundColor= 'red';
    

    Older and minor browsers are likely not to support either syntax. Dynamic stylesheet-fiddling is rarely done because it's quite annoying to get right, rarely needed, and historically troublesome.

    0 讨论(0)
  • 2020-11-22 01:45

    A function to cope with the cross-browser stuff:

    addCssRule = function(/* string */ selector, /* string */ rule) {
      if (document.styleSheets) {
        if (!document.styleSheets.length) {
          var head = document.getElementsByTagName('head')[0];
          head.appendChild(bc.createEl('style'));
        }
    
        var i = document.styleSheets.length-1;
        var ss = document.styleSheets[i];
    
        var l=0;
        if (ss.cssRules) {
          l = ss.cssRules.length;
        } else if (ss.rules) {
          // IE
          l = ss.rules.length;
        }
    
        if (ss.insertRule) {
          ss.insertRule(selector + ' {' + rule + '}', l);
        } else if (ss.addRule) {
          // IE
          ss.addRule(selector, rule, l);
        }
      }
    };
    
    0 讨论(0)
提交回复
热议问题