从Javascript中的extern URL加载时获取图像大小 [英] Get image size when it loads from an extern URL in Javascript

查看:100
本文介绍了从Javascript中的extern URL加载时获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道从extern URL加载时的图像大小,因为项目需要调整图像div的大小。

I need to know the image size when it loads from an extern URL because the project needs to resize the image div.

我需要做类似这样的事情:

I need to do something like this:

<img id="imglegend'+layername+'" src="url_to_an_extern_host" />

并使用Javascript和JQuery:

And, using Javascript and JQuery:

$('#imglegend'+layername).ready(function(){
   var h = $('#imglegend'+layername);
   // Resize image div container
});

但这不起作用。有可能吗?

But this didn't work. Is it possible to do?

提前致谢!

推荐答案

$('#imglegend').load(function(){
   var w =    $(this).width();
   var h =    $(this).height();
   alert(w); alert(h);
}).error(function (){
   $(this).remove();//remove image if it fails to load// or what ever u want
})

这篇关于从Javascript中的extern URL加载时获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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