jQuery的检查是否有元素特定属性

问题描述:

我想看看如果一个元素有一个特定的CSS属性。我想到的是这样的:

I am trying to see if an element have a specific CSS attribute. I am thinking at something like this:

if ( !$(this).attr('font-style', 'italic')) {
alert ("yop")
}
else {
alert ("nope")
}

这是行不通的。在这个任何提示?
谢谢!

It does not work. Any tips on this? Thank you!

下面是一个简单的插件来做到这一点(测试):

Here's a simple plugin to do that (tested):

$.fn.hasCss = function(prop, val) {
    return $(this).css(prop.toLowerCase()) == val.toLowerCase();
}

$(document).ready(function() {
    alert($("a").hasCss("Font-Style", "Italic"));
});

<a style="fonT-sTyle:ItAlIc">Test</a>