简体   繁体   English

如何使用javascript修改CSS样式表(不更改元素样式表)

[英]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样式表,但该样式表不粘滞。

Test Page 测试页

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.

相关问题 如何使用 Javascript 将样式表应用于元素 - How do I apply a stylesheet to an element using Javascript 如何使用下拉框更改样式表? - How do I change stylesheet using a dropdown box? 如何使用 javascript 从父级向 iFrame 添加样式表? - How do I add a stylesheet to an iFrame from the parent, using javascript? 如何使用javascript或jquery返回样式表中样式的css属性的值? - How do i return the value of a css property of a style in the stylesheet using javascript or jquery? 如何通过 javascript 更改样式表 - How To Change stylesheet by javascript 如何修改样式表规则,以使用javascript将后代选择器添加到每个规则的前面? - How can I modify stylesheet rules to add a descendant selector to the front of every rule using javascript? 如何返回外部样式表类应用于元素的所有CSS属性(而不是计算样式!)? - How do I return all CSS properties applied to an element by an external stylesheet class (not get computed styles!)? 如何根据浏览器宽度动态调整css样式表? - How do I dynamically adjust css stylesheet based on browser width? 如何解决CSS样式表引用冲突? 可以本地化吗? - How do i resolve the CSS stylesheet reference conflict? Is localization possible? 如何将样式表元素的值分配给变量? - How do I assign the value of a stylesheet element to a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM