jQuery 确定 CSS 属性是否设置为某个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5928920/
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 20:02:35  来源:igfitidea点击:

Determine if CSS property is set to a certain value?

jqueryattributeshideshow

提问by Tom

Just wondering how to determine a jQuery statement like this

只是想知道如何确定这样的 jQuery 语句

if( $("#test").css('display', 'block') == true) {
   return true;
}

Basically, I want to be able to determine IF an element has is currently being shown or hidden via the "display:block" attribute ?

基本上,我希望能够通过“display:block”属性确定当前是否显示或隐藏元素?

回答by Pekka

Use

if( $("#test").css('display') == 'block') {

I'm fairly sure .css(), returning a calculated value, will always return a lower case result - the docs say nothing on this. To make totally sure, you could do a

我相当肯定.css(),返回一个计算值,总是会返回一个小写的结果 - 文档对此没有任何说明。为了完全确定,你可以做一个

if( $("#test").css('display').toLowerCase() == 'block') {

while you can rely on displaygiving reliable results, note that some CSS properties will not always show up the way they were defined. For example

虽然您可以依靠display提供可靠的结果,但请注意,某些 CSS 属性并不总是按照定义的方式显示。例如

a { color: red }

will turn out rgb(255,0,0);when queried using .css().

会变成rgb(255,0,0);使用查询时.css()

回答by kobe

You can use isvisible and is hidden also

您可以使用 isvisible 和 hidden 也

if ( $('#test').is(':visible')){

回答by sdleihssirhc

I think the only way to test this is by comparing with actual values:

我认为测试这一点的唯一方法是与实际值进行比较:

function displayHidden(elem) {
    return $(elem).css('display') === 'hidden';
}