<分区>
标签 javascript css
<分区>
我想改变 div 类的颜色。我在网上搜索了一些信息,但它对我不起作用。
我有一个 css 样式类,其中我有属性 background-color,我想从 JS 文件更改这个属性。
我想在类 gmBox 中更改 background-color:,如何从 JS 文件中做到这一点?
.gmBox {
vertical-align: middle;
padding: 10px;
margin: 5px;
background-color: yellow;
color: black;
font-size: 23px;
border-radius: 5px;
}
<div class="gmBox" id="gmType">GM12</div>
最佳答案
要更改类,您需要编辑文档样式
[...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.gmBox')
.style['background-color']='red';
[...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.gmBox').style['background-color']='red';
.gmBox {
vertical-align: middle;
padding: 10px;
margin: 5px;
background-color: yellow;
color: black;
font-size: 23px;
border-radius: 5px;
}
<div class="gmBox" id="gmType">GM12</div>
关于javascript - JS 如何从 javascript 更改 css 类颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55467968/