Jquery:添加容器内所有图像元素的宽度
我知道如何做到这一点,但我想知道是否有人有更好的想法或可以帮助我。我有一个无序列表,其中动态生成了不同数量的图像。我想添加每个图像的宽度并将包含的无序列表宽度设置为该值。
例如,如果输出三个图像,则 html 可能如下所示:
<ul id="thumbnails">
<li><a href="#"><img src="image_path"></a></li>
<li><a href="#"><img src="image_path"></a></li>
<li><a href="#"><img src="image_path"></a></li>
</ul>
如果图像一为 200px,图像二为 100px,图像三为 50px,我想将缩略图 ul 的宽度指定为 350px。
$('#thumbnails').css('width', '350px');
不过,每个图像都在订单项上应用了 2 像素的右边距,因此我也想将其添加到图像中。因此,如果生成 3 个图像,我希望总宽度为 356px。
感谢大家的帮助。我一直在研究 jquery 的each() 和width() 函数来完成此任务。
I have an idea of how I might do this but I was wondering if anyone had a better thought or could help me out. I have an unordered list with a varying number of images dynamically generated within them. I'd like to add the width of each image and set the containing unordered list width to that value.
For example, if three images were output the html might look like this:
<ul id="thumbnails">
<li><a href="#"><img src="image_path"></a></li>
<li><a href="#"><img src="image_path"></a></li>
<li><a href="#"><img src="image_path"></a></li>
</ul>
If image one was 200px, image two was 100px, and image three was 50px, I'd like to assign the width of the thumbnails ul to 350px.
$('#thumbnails').css('width', '350px');
Each image does have a 2px margin-right applied to the line item though, so I'd like to add this to the image as well. So if 3 images were generated I'd like the total width to be 356px.
Thanks all for any help. I've been looking at jquery's each() and width() functions to accomplish this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您的代码。为了让它与 Safari 和 Chrome 一起工作,我需要使用窗口加载来加载它,而不是 dom 准备好。不确定这种方法的缺点是什么,但我猜他们是这样的,因为 dom read 更常见。
Thanks for the code. To get it working with Safari and Chrome I needed to load it with window load not dom ready. Not sure what the downside to this method is but im guessing their is one as dom ready is more common.