0

次の関数を使用すると、周囲の div に img 幅を適用できます。

$(document).ready(function(){

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

});

問題は、img に width 属性がある場合にのみ機能することです。imgタグに幅属性がない場合でも、周囲の.imagecontainer divにimg幅を追加するにはどうすればよいですか? load() 関数を使用して画像の幅を表示できますが、それを div の幅に適用する方法がわかりません。

4

2 に答える 2

0

使用する

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

これはうまくいきます

于 2013-01-26T09:42:08.127 に答える
0

usingattrは属性値を与え、定義されていない場合は機能しません。使用できます

   $(this).width = $(this).find('img').width;
于 2013-01-26T09:47:40.450 に答える