jQuery check if font weight is normal or bold

2019-06-19 02:37发布

问题:

I'm setting the font-weight property with the following code:

$(this).css({ 'font-weight': 'normal' });

Now i want to check if an element has bold or normal font-weight propety, how do i do this?

回答1:

you can get it using:

fontWeight = $(this).css('font-weight')

To compare:

if (fontWeight  == 'normal'|| fontWeight  == '400')
//or
if (fontWeight  == 'bold' || fontWeight  == '700')


回答2:

You can use:

if ($('#yourElement').css('font-weight') == 'normal') {
    // Your code if font-weight is normal
} else if($('#yourElement').css('font-weight') == 'bold') {
    // Your code if font-weight is bold
}