简体   繁体   中英

jQuery - test if img width is 'more than' and 'less than'

I would like to get action if with attr of an image is between two values : On my code this .w50 is just on the width attr value = 470px

$("img").each(function() {

  var image = $(this);
  if (image.attr("width") == 470) {
      image.parent().addClass('w50');
  }

});

how can I write "add this class if attr value is more than 470 and less than 490" ?

Try the following:

if ((image.attr("width") >= 470) && (image.attr("width") <= 490))

Note that I have included both 470 and 490 because I think this is what you meant.. if you don't need the edges drop both = .

More details:

The && (AND) operator means that both conditions have to be true.

>= means greater than or equal.

<= means less than or equal.

You can read more about comparison operators and logical operators .

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