jQuery check if element have specific attribute

后端 未结 6 757
忘了有多久
忘了有多久 2021-01-06 03:07

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         


        
6条回答
  •  Happy的楠姐
    2021-01-06 03:49

    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"));
    });
    
    Test
    

提交回复
热议问题