我在一个页面上有一堆图像。我正在尝试使用 jQuery 来获取每个图像的高度并将其显示在图像之后。所以这是我的代码:
$(document).ready(function() {
$(".thumb").each(function() {
imageWidth = $(".thumb img").attr("width");
$(this).after(imageWidth);
});
});
<div class="thumb"><img src="" border="0" class="thumb_img"></div>
<div class="thumb"><img src="" border="0" class="thumb_img"></div>
<div class="thumb"><img src="" border="0" class="thumb_img"></div>
[...]
我在 jQuery 背后的逻辑是我想遍历每个“thumb”选择器,将“thumb”内的 img 高度分配给变量“imageWidth”,然后在每个“thumb”之后以文本显示高度。
我遇到的问题是它只在第一张图像上工作,然后退出。当然,如果我使用“thumb_img”类,我可以让它工作,但我需要访问“thumb”类的图像高度。
希望这不会太令人困惑,因为我对 jQuery 还很陌生。提前谢谢。