CSS!important似乎在jQuery / Firefox 3.x中不起作用

前端之家收集整理的这篇文章主要介绍了CSS!important似乎在jQuery / Firefox 3.x中不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

见小提琴:http://jsfiddle.net/csaltyj/P2sLa/

在Chrome中,这种方法很好,但在Firefox中却没有,并产生Javascript警告.我知道这里不需要!important,但我有一个需要的大型项目,这导致Firefox崩溃.有任何想法吗?

最佳答案
搜索“jquery css important”提出了a blog post explaining the “problem”.
我会发布一些额外的信息,但长话短说,here’s your solution

$('#set-bg').click(function() {
    $('#Box').css('cssText','background: blue !important');
});

作者指出:

This is not a bug but something that
most browser doesn’t acknowledge a
need since inline style already have
the highest priority other than user
defined ones. (unless you do not want
your user to change how they view your
website).

请注意,使用cssText有一个缺点,您可能需要考虑
改为使用cssRules

cssText will overwrites the css of
that particular element. Hence,you
will have to redeclare the rule for
that particular element all over
again.

尽管如此.请听听作者的总结:

Using !important in CSS is not
advisable since it might kill your web
usability. Furthermore,there is no
reason why !important should be use as
inline styling already has the highest
priority. Hence,you might want to
reconsider applying !important on your
script after thinking about the
consequences that might bought to your
users.

猜你在找的CSS相关文章