我有一个像这样的 div 元素:
<div id="test">
<img src="..."/>
<img src="..."/>
<img src="..."/>
<img src="..."/>
</div>
我想做的是首先使所有图像具有相同的高度,然后计算它们的新宽度之和。我试过这个:
window.onload = function() {
var width = $(window).width(), totalwidth=0;
$('#test img').height('200px');
$("#test").each(function() {
totalwidth += $("#test img").attr("width");
});
alert(totalwidth);
}
但警报返回 NaN
。我该怎么办?
最佳答案
尝试
$("#test img").each(function() {
totalwidth += $(this).width();
});
关于javascript - 如何计算div中许多图像的总宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989158/