jquery设置display失效问题,当样式中存在!important时无法使用css()、show()或hide()

[ 2024-04-13 22:27:09 | 作者: admin ]
字号: | |
如果使用!important在你的样式中,比如display: none !important,此时就不能用show()了

但是我在查阅资料时发现有这样的解释,
If using !important in 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.


这其实是不对的(也许是因为jquery版本不同的原因)

我查了下官方文档并测试了下,应该是这样:
第一种方法,直接设置style,不能用.css()和.prop()
$("#a1").attr("style","display:block !important");
不能用
$("#a1").css("display","block")

例子:http://jsfiddle.net/FF3mc/3/
第二种方法(前提是原有的显示属性是在类选择器里):通过改变类名间接改变元素的显示,如addClass removeClass toogleClass方法;

例子:http://jsfiddle.net/FF3mc/4/
就是说没有show()的什么事了,并不是所谓的"希望.show()方法正常工作".

引用的资料来源:https://stackoverflow.com/questions/12913573/show-elements-with-style-displaynone-important-jquery

转载于:https://www.cnblogs.com/wincent98/p/7135882.html
[最后修改由 admin, 于 2024-04-13 22:34:06]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=2964

这篇日志没有评论。

此日志不可发表评论。