显示具有样式 display:none !important (jQuery) 的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12913573/
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
show elements with style display:none !important (jQuery)
提问by user1643156
As said in jQuery documentation --- .show()
Note: If using !importantin your styles, such as display: none !important, it is necessary to override the style using .css('display', 'block !important')should you wish for .show() to function correctly.
注意:如果在您的样式中使用!important,例如display: none !important,如果您希望 .show() 正常运行,则有必要使用.css('display', 'block !important')覆盖样式 .
But it seems the style cannot be overridden and the element cannot be shown. Am I doing anything wrong here?
但似乎样式不能被覆盖,元素也不能显示。我在这里做错了什么吗?
回答by bart s
When you use the attr
it can work. jQueries .css()
and .prop()
both won't work
当您使用attr
它时,它可以工作。jQueries.css()
和.prop()
两者都不起作用
In this fiddle with attr
remove the !important
from the javascript and you will see how the CSS !important
overrules even the inline styling.
在这个小提琴attr
中,!important
从 javascript 中删除,您将看到 CSS 如何!important
推翻甚至内联样式。
回答by manavo
Alternatively you can do it with classes (so all your CSS is in the CSS files themselves, and not being directly altered by javascript): http://jsfiddle.net/FF3mc/4/
或者你可以用类来做(所以你所有的 CSS 都在 CSS 文件中,而不是被 javascript 直接改变):http: //jsfiddle.net/FF3mc/4/
回答by Aufuray
$('.classname').attr("style", "display: block !important");
We can't use .css() here as it does not support !important.
我们不能在这里使用 .css() 因为它不支持 !important。