使用 javascript 更改整个 CSS 类的样式
有没有办法使用 javascript 更改 CSS 类的属性?
<style type="text/css">
.fool select {
display: block;
}
</style>
<p class="fool">
<select id="a" onchange="changeCSS()"> ... </select>
<select id="b" > ... </select>
<select id="c" > ... </select>
</p>
我想在用户调用函数changeCSS()后将所有
它看起来很简单,但我找不到方法来做到这一点......
Is there a way to change an attribute of a CSS class using javascript?
<style type="text/css">
.fool select {
display: block;
}
</style>
<p class="fool">
<select id="a" onchange="changeCSS()"> ... </select>
<select id="b" > ... </select>
<select id="c" > ... </select>
</p>
I want to change display:block to display:none for ALL <select>
elements after a user call function changeCSS().
It looks simple but I can't find a way to do this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我是这样做的:
Here's how I'd do it:
要获得相同的效果,您可以制作另一种样式:
并将
的类更改为
foolnone
。否则,您必须遍历
的每个子级并更改类。如果这就是您想要的方式,可能最好使用一些库,例如 jquery。有了它,您可以执行以下操作:
请参阅此 jsfiddle 以获取工作示例:
这显示了上述两种方法(即隐藏整个
并隐藏每个
To get the same effect, you can make another style:
and change the class of
<p>
tofoolnone
.Otherwise, you'd have to go through each of the children of
<p>
and change the class. If that's the way you want to go, probably probably best to use some library, such as jquery. With it, you can do something like:See this jsfiddle for a working example:
This shows both above approaches (i.e. hiding the whole
<p>
and hiding each of the<select>
s.我认为如果这样写的话会起作用:
I think it would work if instead put like this:
您可以修改样式规则,但这通常不是最佳的设计决策。
要访问样式表定义的样式规则,请访问
document.styleSheets
集合。该集合中的每个条目都将具有一个名为cssRules
或rules
的属性,具体取决于浏览器。其中每一个都将是CSSRule
实例。您可以通过更改其cssText
属性来更改规则。但同样,这可能不是解决问题的最佳方法。但这是你问题的字面答案。
解决该问题的最佳方法可能是在样式表中添加另一个类来覆盖先前规则的设置,然后将该类添加到
select
元素或它们的容器中。例如,您可以制定规则:...当您想要隐藏选择时,将
"bar"
类添加到具有"fool"
的容器中> 类。或者,将 CSS 样式信息直接应用于元素。
You can modify style rules, but it's usually not the best design decision.
To access the style rules defined by style sheets, you access the
document.styleSheets
collection. Each entry in that collection will have a property either calledcssRules
orrules
depending on the browser. Each of those will be aCSSRule
instance. You can change the rule by changing itscssText
property.But again, that's probably not the best way to solve the problem. But it is the literal answer to your question.
The best way to solve the problem is probably to have another class in your stylesheet that overrides the settings of the previous rule, and then to add that class either to the
select
elements or to the container of them. So for instance, you could have the rules:...and when you want to hide the selects, add the
"bar"
class to the container that has the"fool"
class.Alternately, apply CSS style information directly to elements.
关键是为额外的类定义额外的规则并将这些类添加到元素中,而不是为给定的样式规则重写规则。
JS
CSS
或者一个真正有效的方法(但可能也需要一些更具体的样式规则)
JS
CSS
The key is to define extra rules for additional classes and add these classes to the elements rather than to rewrite the rules for a given style rule.
JS
CSS
Or for a really efficient method (but which might need a few more specific style rules too)
JS
CSS
我直接访问 CSS 类来同时调整一堆 div 的高度。我就是这样做的:
然后你可以执行类似
css_getclass('.classname').style.background="blue"
的操作。我只在 Windows 版 chrome 上尝试过这个,祝其他浏览器好运。I'm accessing CSS classes directly to adjust the height of a bunch of divs simultaneously. This is how I'm doing it:
and then you can do something like
css_getclass('.classname').style.background="blue"
. I only tried this on chrome for windows, good luck with other browsers.您可以编辑样式表,
但我认为你只需创建一个新类即可。
如果你真的需要这样做,试试这个。
cssRules 是样式表规则
第一个规则的选择器
读取第一条规则的样式
更改样式
CSS2Properties { "margin-left" → "15px" }
You can edit the style sheets,
but I would think you would just create a new class instead.
If you really need to do it, try this.
cssRules Are the style sheet rules
Selector for the first rule
Read the style of the first rule
Change the style
CSS2Properties { "margin-left" → "15px" }