开发者

get image width & height

开发者 https://www.devze.com 2023-02-06 01:03 出处:网络
I am building website,i want to get image size i.e.width or height using jquery. here is my code, var photograf=(\'url(upload/\'+ nm +\'.jpg)\'开发者_如何学运维);

I am building website,i want to get image size i.e.width or height using jquery. here is my code,

var photograf=('url(upload/'+ nm +'.jpg)'开发者_如何学运维);
var imgwidth=photograf.width();
var imgheight=photograf.height(); 

but it's not working.i know it's wrong but i have do above thing. please help me other or correct way. thank you.


You're initializing photograf as a string, which has no width() or height() functions. Nowhere are you actually attempting to load an image, with jQuery or otherwise. In fact, there is no jQuery anywhere in your code.

You could either add the image to the page, and then find the resulting element's width/height, or create a new Image object:

photograf = new Image();
photograf.src = 'upload/' + nm + '.jpg');
var width = photograf.width;
var height = photograf.height;


Given that you tagged with jquery, this is the jquery way to do what meagar has written:

var photograf = $("<img src='upload'"+nm+".jpg />");
var imgWidth = photograf.width;
var imgHeight = photograf.height;
0

精彩评论

暂无评论...
验证码 换一张
取 消