[英]How do I modify a CSS stylesheet using javascript (NOT how to I change an element's stylesheet)
I am trying to modify a CSS stylesheet by setting its cssText attribute but it isn't sticking. 我试图通过设置CSS样式表的cssText属性来修改CSS样式表,但该样式表不粘滞。
Pushing the button should toggle its appearance from a red arrow to a black arrow and black but nothing changes. 按下按钮应将其外观从红色箭头切换为黑色箭头,然后将黑色切换为黑色,但没有任何变化。 This appears in the javascript log:
这将显示在javascript日志中:
changed cssText from .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } to .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } should be .movebutton button { width: 100px; height: 100px; background-image: url("blackarrow.svg"); } changed cssText from .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } to .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } should be .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } changed cssText from .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } to .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } should be .movebutton button { width: 100px; height: 100px; background-image: url("blackarrow.svg"); } changed cssText from .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } to .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); } should be .movebutton button { width: 100px; height: 100px; background-image: url("redarrow.svg"); }
So basically it seems as if the change is not sticking. 因此,基本上看起来变化没有持久。 This is in Firefox 7.0.1.
这是Firefox 7.0.1中的版本。
Why are you trying to change the style definition at runtime?.. The faster, easier and cleaner way to achieve this is to define your styles in the CSS definition: 为什么要在运行时尝试更改样式定义?。更快,更轻松,更干净的方法是在CSS定义中定义样式:
.movebuttonRed { width: 100px; height: 100px; background-image: url(http://www.tupari.net/jstest/redarrow.svg);}
.movebuttonBlack { width: 100px; height: 100px; background-image: url(http://www.tupari.net/jstest/blackarrow.svg);}
And then change the elements className attribute using Javascript - 然后使用Javascript更改元素的className属性-
if (document.getElementById("mybutton").className == "movebuttonRed") {
document.getElementById("mybutton").className = "movebuttonBlack";
} else {
document.getElementById("mybutton").className = "movebuttonRed";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.