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

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

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

有问题的代码是:

var theImage = new Image(); theImage.src = $image.attr('src'); var imgwidth = theImage.width; var imgheight = theImage.height; 

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

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

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

http://jsfiddle.net/cyrilkong/XJUGt/4/

 function imgRealSize(img) { var $img = $(img); if ($img.prop('naturalWidth') === undefined) { var $tmpImg = $('').attr('src', $img.attr('src')); $img.prop('naturalWidth', $tmpImg[0].width); $img.prop('naturalHeight', $tmpImg[0].height); } return { 'width': $img.prop('naturalWidth'), 'height': $img.prop('naturalHeight') }; } $(function() { var target = $('img.dummy'); var target_native_width; var target_native_height; target.each(function(index) { // console.log(index); imgRealSize(this[index]); target_native_width = $(this).prop('naturalWidth'); target_native_height = $(this).prop('naturalHeight'); $(this).parent('div').append('

This image actual size is ' + target_native_width + ' x ' + target_native_height + ', and css is ' + $(this).width() + ' x ' + $(this).height() + '.

'); }); });​