开发者

google chrome, can't fix img width, height

if i have an image element where i'm not set height and width, like this

<img id="img" src="image.jpg" />

and then when i try to get

开发者_如何学Python
img_height = $("#img").attr("height");

it returns 0.

how can i get the size of image in google chrome.

Thanks

Update:

height = $("#img").height(); doesn't work too...


You can use the .height() and .width() methods, like this:

var height = $("#img").height();
var width= $("#img").width();

Also make sure you're running this in $(window).load() rather than $(document).ready() so that the image is loaded.


You can use jQuery's .height() & .width()

EDIT: Now with caching to make the internet a better place:

var img_height, img_width, the_img = $("#img");

img_height = the_img.height();
img_width = the_img.width();


That returned 0 because the attribute 'height' is not set. Try this jQuery function instead:

img_height = $("#img").height();


Does this work in other browsers? Your HTML does not indicate a specified "height" attribute.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜