我有一个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);
})
})
})
});