如果我有CSS:
.myclass
{
background: #FF00FF;
}
和 html:
<div class="myclass">etc.</div>
<div class="myotherclass">etc.</div>
<div class="myclass">etc.</div>
如何将 myclass 的背景变量更改为另一种颜色,以便该类的所有 div 的背景都发生变化?
请不要jquery,谢谢
最佳答案
给你:
[].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;
});
});
关于javascript - 如何动态更改样式表中 CSS 属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797546/