简体   繁体   中英

Do something in jquery if element has exact property & value

I want to do something in jquery if my element has a given property & value.

This is what I'm trying that doesn't work:

if ($('.myElement').css('opacity','1')){
    console.log('test');
}

What is the correct syntax for this?

The second paramter of css() sets the style, not passing that parameter returns the current style for you to check against a string :

if ($('.myElement').css('opacity') == '1'){
    console.log('test');
}

To get the css style we use $(element).css(propertyname);

to set style we use $(element).css(propertyname,value);

if ($('.myElement').css('opacity') == 1){
    console.log('test');
}

.css()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM