How to dynamically change the value of a CSS property inside a stylesheet?

前端 未结 2 728
走了就别回头了
走了就别回头了 2021-01-06 17:57

If I have css:

.myclass
{
background: #FF00FF;
}

And html:

etc.
2条回答
  •  清酒与你
    2021-01-06 18:16

    Here you go:

    [].every.call( document.styleSheets, function ( sheet ) {
        return [].every.call( sheet.cssRules, function ( rule ) {
            if ( rule.selectorText === '.myclass' ) {
                rule.style.backgroundColor = 'green';
                return false;
            }
            return true;
        });
    });
    

    Live demo: http://jsfiddle.net/gHXbq/

    ES5-shim for IE8

提交回复
热议问题