【问题标题】:jQuery - test if img width is 'more than' and 'less than'jQuery - 测试 img 宽度是否“大于”和“小于”
【发布时间】:2015-02-10 01:07:30
【问题描述】:

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

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

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

});

如何写“如果 attr 值大于 470 且小于 490,则添加此类”?

【问题讨论】:

    标签: jquery html image attr


    【解决方案1】:

    尝试以下方法:

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

    请注意,我将 470 和 490 都包括在内,因为我认为这就是您的意思.. 如果您不需要边缘,请同时删除 =

    更多细节:

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

    &gt;= 表示大于或等于。

    &lt;= 表示小于或等于。

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

    【讨论】:

    • 应该是&amp;&amp;,而不是||
    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多