set style with :hover javascript

前端 未结 2 1164
失恋的感觉
失恋的感觉 2021-02-06 01:55

I understand that the following method is great for setting CSS styles because of browser compatibility.

element.style.cssText = \"color:red;\";
<
2条回答
  •  孤街浪徒
    2021-02-06 02:14

    You can do it with some Voodoo magic:

    var head = document.getElementsByTagName('head')[0];
    var style = document.createElement('style');
    var declarations = document.createTextNode('selector:pseudo { property: value }');
    
    style.type = 'text/css';
    
    if (style.styleSheet) {
      style.styleSheet.cssText = declarations.nodeValue;
    } else {
      style.appendChild(declarations);
    }
    
    head.appendChild(style);
    

    Not exactly what you needed, but you can tweak it and make a fancy function out of it if you want.

提交回复
热议问题