有没有办法来引用一个现有的CSS样式从另一个CSS样式使用CSS或javascript? [英] Is there a way to reference an existing CSS style from another CSS style using CSS or javascript?
问题描述
如果我有一个定义的样式
If I have a style defined
.style1
{
width: 140px;
}
我可以从第二种风格参考吗?
can I reference it from a second style?
.style2
{
ref: .style1;
}
还是有通过javascript / jQuery的方式?
Or is there a way via javascript/jQuery?
---编辑
为了澄清问题,我试图应用为#x和#c定义的任何样式.x和.c而不改变CSS,因为CSS将有我不能控制的更新。
To clarify the problem, I am trying to apply whatever style is defined for a #x and #c to .x and .c without altering the CSS as the CSS is going to have updates that are out of my control.
我使用宽度,但真正的样式将是更多的
I used width but really the style would be something more complex with font, border and other style elements being specified.
当样式应用于类时,指定多个类名称会起作用,所以我将现有响应标记为答案,但我需要采取应用于id的风格,并将其应用于类样式...如果这是有道理的。
Specifying multiple class names does work when the style is being applied to a class so I'll mark existing responses as answers, but I need to take the style being applied to an id and also apply it to a class style ... if that makes any sense.
推荐答案
没有办法使用CSS - 这是一个请求的功能,但不包括在规范中。你也不能用JS直接做,但是有一个怪异的解决方法:
There's no way to do it with CSS -- it's an oft-requested feature, but not included in the spec yet. You also can't do it directly with JS, but there's sort of a hacky workaround:
$('.style2').addClass ('style1');
这篇关于有没有办法来引用一个现有的CSS样式从另一个CSS样式使用CSS或javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!