1 つの親に 4 つの div があり、それぞれに 1 つの画像が含まれています。それぞれのdivでその特定の画像の幅を表示したい。しかし、私の関数はすべてのコンテナの最後の画像幅を返します。
<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$('.box').each(function () {
var height = $(this).find('img').height();
var width = $(this).find('img').width();
$('.width').text(width)
})
})
</script>
<style>
.box {
float:left;
margin-left:20px;
position:relative
}
.width {
position:absolute;
left:0;
top:0;
color:#FFF;
font-size:20px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<div class="width"></div>
<img src="img/sample1.jpg" height="200" width="300" />
</div>
<div class="box">
<div class="width"></div>
<img src="img/sample2.jpg" height="200" width="350" />
</div>
<div class="box">
<div class="width"></div>
<img src="img/sample3.jpg" height="200" width="150" />
</div>
<div class="box">
<div class="width"></div>
<img src="img/sample4.jpg" height="200" width="100" />
</div>
</div>
</body>