jQuery 整个 css“.class” 是否可以像 !important 一样具有特殊性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16501658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can an entire css ".class" have it's specificity as !important?
提问by MacLovin
I am working heavily in jQuery UI widgets and themeing is becoming hard because of an oversight that I need to work around.
我在 jQuery UI 小部件方面做了大量工作,并且由于我需要解决的疏忽,主题化变得越来越困难。
Consider this problem:
考虑这个问题:
<div id="node" class="ui-widget">
<div class="ui-widget-content">
</div>
</div>
then this jquery
那么这个jquery
<script>
$(function() {
$('#node').hover(function (){
$(this).toggleClass("ui-state-error");
});
});
</script>
I would like the ui-state-error to be !important to the nested div. This is pseudo code, but I find large examples of this happening where containers have css swaps and children (basicaly) ignore it. What is even worse is this: if I would like to be able to overwrite in jQuery say "backgroundcolor=ui-state-error:background-color knowing 100% it all won't go to pieces because I don't "know" necessarily that that background is the important one for the element in question.
我希望 ui-state-error 对嵌套的 div 很重要。这是伪代码,但我发现在容器具有 css 交换和子项(基本上)忽略它的情况下发生这种情况的大量示例。更糟糕的是:如果我希望能够在 jQuery 中覆盖,请说“backgroundcolor=ui-state-error:background-color 知道 100% 这一切都不会分解,因为我不“知道”必然是该背景对于所讨论的元素是重要的。
here is a fiddle of a case in point http://jsfiddle.net/WCuYH/1/
这是一个例子http://jsfiddle.net/WCuYH/1/
EDIT I am going to recommend this question is closed because there hasn't been any attempts at answers, only too much drift from the essence of the original post
编辑我将建议关闭这个问题,因为没有任何答案尝试,只是与原始帖子的本质有太多的偏差
回答by jfriend00
First off !important
applies to one specific declaration in a CSS rule. It doesn't apply to a class. So, "no" you can't make a class !important
.
首先!important
适用于 CSS 规则中的一个特定声明。它不适用于类。所以,“不”你不能创建一个类!important
。
Second off, !important
is just one part of CSS specificity. You can also use other ways to make a rule be a more specific rule to have precedence (such as referring to an id in the parent chain instead of just the class. When I'm writing CSS, using !important is my last possible choice - I'd much rather solve overrides with other specificity solutions. Usually, if you control all the CSS, this is pretty easy to avoid using !important
. If you have to override some CSS that you don't control, then sometimes it is handy.
其次,!important
这只是 CSS 特性的一部分。您还可以使用其他方法使规则成为具有优先级的更具体的规则(例如引用父链中的 id 而不仅仅是类。当我编写 CSS 时,使用 !important 是我最后可能的选择- 我更愿意使用其他特殊性解决方案来解决覆盖问题。通常,如果您控制所有 CSS,则很容易避免使用!important
。如果您必须覆盖一些您无法控制的 CSS,那么有时它会很方便。
If your jQuery code is going to toggle a class on the object, then you will have to craft your CSS rules so that adding/removing this class causes the right CSS declarations to have precedence. For us to help you further with that part, you would need to show us the relevant parts of both your HTML and CSS so we could advise on how to solve your precedence/specificity problem.
如果您的 jQuery 代码要在对象上切换一个类,那么您必须制定 CSS 规则,以便添加/删除此类会导致正确的 CSS 声明具有优先权。为了让我们在该部分进一步帮助您,您需要向我们展示您的 HTML 和 CSS 的相关部分,以便我们就如何解决您的优先级/特异性问题提供建议。