jQuery检查元素是否具有特定属性 [英] jQuery check if element have specific attribute

查看:17
本文介绍了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>

这篇关于jQuery检查元素是否具有特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆