jQuery 检查元素是否具有 css 属性

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

jQuery check if element has css attribute

jqueryattributes

提问by Mircea

I need to know when I click on an element if this element has a CSS attribute. I am thinking of something like this, but it does not work:

我需要知道当我单击某个元素时该元素是否具有 CSS 属性。我在想这样的事情,但它不起作用:

if ($('#element').attr("text-shadow")) {
    alert ('i Have')
}
else {
    alert ('i dont')
}

Any tips on this one? Thanx

关于这个有什么提示吗?谢谢

回答by Stefan Kendall

if( $('#element').css('text-shadow') != null )  { 
    /*success*/ 
} 
else { 
    /*does not have*/ 
}

回答by yuliskov

In case you want to test 'width' style property. Behavior in Firefox is slightly differ from other browsers.

如果您想测试 'width' 样式属性。Firefox 中的行为与其他浏览器略有不同。

if (jQuery(value).css('width') == '0px') { // width not set
    jQuery(value).css('width', '320px');
}

回答by prendio2

$('#element').css("text-shadow")

$('#element').css("text-shadow")

回答by Jimmy Baker

How about this instead:

这个怎么样:

if($('#element').css('text-shadow') == null) ...

回答by Khanh Huynh

How about this

这个怎么样

if ($('#element')[0].style.text-shadow != '') {
    alert ('i Have')
}
else {
    alert ('i dont')
}