显示具有样式 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:07:27  来源:igfitidea点击:

show elements with style display:none !important (jQuery)

jquerycssshow

提问by user1643156

As said in jQuery documentation --- .show()

正如jQuery 文档中所说的--- .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?

但似乎样式不能被覆盖,元素也不能显示。我在这里做错了什么吗?

jsfiddle

提琴手

回答by bart s

When you use the attrit can work. jQueries .css()and .prop()both won't work

当您使用attr它时,它可以工作。jQueries.css().prop()两者都不起作用

In this fiddle with attrremove the !importantfrom the javascript and you will see how the CSS !importantoverrules even the inline styling.

在这个小提琴attr中,!important从 javascript 中删除,您将看到 CSS 如何!important推翻甚至内联样式。

your fiddle

你的小提琴

回答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。