如何使用jQuery获取实际图像宽度和高度? [英] How do I get actual image width and height using jQuery?

查看:126
本文介绍了如何使用jQuery获取实际图像宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面上,我显示了100张图片,其宽度为
,高度属性已更改。图像ID处于循环中。

On a page I have displayed 100 images, which have the width and height attribute changed. Image id is in a loop.

如何在此循环中获得原始图像大小?

How do I get the original image size, inside this loop?

$(this).appendTo(".prod_image").attr('height',150).attr('width',150).attr('title','').attr('name','').attr('alt','').attr('id','id'+i);


推荐答案

您可以使用以下方法检索图像的原始尺寸dom对象的naturalWidth和naturalHeight属性。

You can retrieve the original dimensions of an image by using the naturalWidth and naturalHeight properties of the dom object.

例如。

var image = document.getElementById('someImage');

var originalWidth = image.naturalWidth; 
var originalHeight = image.naturalHeight;

使用jQuery看起来像:

With jQuery it would look like:

var image = $('#someImage');

var originalWidth = image[0].naturalWidth; 
var originalHeight = image[0].naturalHeight;

这篇关于如何使用jQuery获取实际图像宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆