使用jQuery获取图像大小 [英] Get image size with Jquery
问题描述
我有一张图片列表
<img src="001.jpg">
<img src="002.jpg">
<img src="003.jpg">
<img src="004.jpg">
<img src="005.jpg">
每个图像的宽度为200像素,但高度不同.有没有一种方法可以使用Jquery查找,然后在每个图像加载后设置它们的高度?
Each image is 200px wide but the heights are different. Is there a way with Jquery to find, and then set the height of each image after they load?
我打算在单个页面上包含数十个图像,并且不想将width和height属性添加到每个图像标签中.
I plan on having dozens of images on a single page and don't want to add the width and height attribute to every single image tag.
我正在使用Masonry插件,它需要图像的width和height属性.
I am using the Masonry Plugin and it requires a width and height attribute for images.
推荐答案
我相信这会满足您的要求:
I believe this will do what you want:
$('img').each(function() {
$(this).attr('height',$(this).height());
$(this).attr('width',$(this).width());
});
您可能还希望为每个img添加一个class="masonry"
属性,然后将选择器设置为$('img.masonry')
.
You will probably also want to add a class="masonry"
attribute to each img as well and then make your selector $('img.masonry')
.
这篇关于使用jQuery获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!