The problem is caused by jQuery not understanding the !important
attribute, and as such fails to apply the rule.(问题是由jQuery无法理解!important
属性引起的,因此无法应用该规则。)
You might be able to work around that problem, and apply the rule by referring to it, via addClass()
:(您可能可以解决该问题,并通过addClass()
引用它来应用规则:)
.importantRule { width: 100px !important; }
$('#elem').addClass('importantRule');
Or by using attr()
:(或使用attr()
:)
$('#elem').attr('style', 'width: 100px !important');
The latter approach would unset any previously set in-line style rules, though.(但是,后一种方法将取消设置任何先前设置的内联样式规则。) So use with care.(因此,请谨慎使用。)
Of course, there's a good argument that @Nick Craver's method is easier/wiser.(当然,有一个很好的论据,即@Nick Craver的方法更容易/更明智。)
The above, attr()
approach modified slightly to preserve the original style
string/properties, and modified as suggested by falko in a comment:(上面的attr()
方法略作修改以保留原始style
字符串/属性,并按照falko在评论中的建议进行了修改:)
$('#elem').attr('style', function(i,s) { return (s || '') + 'width: 100px !important;' });
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…