【问题标题】:jQuery detect image sizejQuery检测图像大小
【发布时间】:2013-01-10 05:39:36
【问题描述】:

通过以下函数,我可以将 img 宽度应用于其周围的 div:

$(document).ready(function(){

  $('.imagecontainer').each(function() {
    $(this).width( $(this).find('img').attr('width') );
  });

});

问题在于,这仅在 img 具有 width 属性时才有效。即使 img 标签没有宽度属性,如何将 img 宽度添加到周围的 .imagecontainer div 中?我可以使用 load() 函数显示图像宽度,但没有胶水如何将其应用于 div 宽度。

【问题讨论】:

    标签: jquery image image-size


    【解决方案1】:

    使用attr 给出属性值,如果未定义,它将不起作用。你可以使用

       $(this).width = $(this).find('img').width;
    

    【讨论】:

      【解决方案2】:

      使用

      $(this).find('img').width()
      

      这会奏效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 2010-11-11
        相关资源
        最近更新 更多