div
画像とキャプションを含める画像ラッパーがあります。
以下を使用して、イメージ ラッパーdiv
のサイズをイメージの幅に合わせます。
$('.imgright').each(function(){
$(this).width($(this).find('img').outerWidth());
});
div
親クラスに依存している場合は、別の画像パスも使用したいと考えています。以下の作品:
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
});
});
しかし、これらを一緒にすると、イメージ ラッパーの幅が常に設定されるわけではありませんdiv
。
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
});
});
$('.imgright').each(function(){
$(this).width($(this).find('img').outerWidth());
});
助言がありますか?
2 つの非常に簡単な回答のおかげで、コードを再構築し、.load()
関数を使用して変更された画像の詳細を取得しました。
作業コードは次のとおりです。
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
$(this).load(function(args){
var newWidth = $(this).outerWidth();
$(this).parent('.imgright').each(function(){
$(this).width(newWidth);
})
})
})
});