jQuery的; 新图片()的Chrome图片宽度和高度= 0

Tou*_*shi 3 jquery google-chrome image phpthumb

我无法让Chrome在DOM加载后识别图像宽度或高度.Image通过phpThumb脚本(调整图像大小)动态加载.如果我拿走动态网址并将其替换为图片的直接网址我没有任何问题,一切都可以在Chrome中运行但是使用动态网址时,Chrome似乎无法计算图片的宽度或高度.

有人对此有经验吗?它正在努力.

有问题的代码是:

var theImage     = new Image();
theImage.src     = $image.attr('src');
var imgwidth     = theImage.width;
var imgheight    = theImage.height;
Run Code Online (Sandbox Code Playgroud)

其中imgwidth = 0; 对于chrome,但IE,Firefox都报告了正确的大小.

mik*_*ike 5

正确的代码是.onload和以下函数:

var theImage     = new Image();
theImage.src     = $image.attr('src');
theImage.onload = function(){
    var imgwidth     = $(this).width;
    var imgheight    = $(this).height; 
});
Run Code Online (Sandbox Code Playgroud)