jQuery - 测试 img 宽度是否为 'more than' 和 'less than'

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

如果图像的 attr 介于两个值之间,我想采取行动:在我的代码中,这个 .w50 正好在宽度 attr 值 = 470px

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

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

});

我怎么写"add this class if attr value is more than 470 and less than 490"?

尝试以下操作:

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

请注意,我同时包含了 470 和 490,因为我认为这就是你的意思.. 如果你不需要边缘,则同时删除 =.

更多详情:

&& (AND) 运算符意味着两个条件都必须为真。

>=表示大于等于。

<=表示小于等于

您可以阅读有关 comparison operators and logical operators 的更多信息。