无法通过jquery获取ajax加载图像的高度和宽度

我通过ajax像这样外部加载图像……

function load_image(image_href) { var img = new Image(); $(img).load(function () { $(this).hide(); $('#a_box').append(this); $(this).fadeIn(); }, gallery_image_load_complete() ).error(function () { }).attr('src', image_href); } function gallery_image_load_complete() { conslole.log('complete') $('#a_box img').height(); //wrong numbers, as though the image is partially loaded $('#a_box img').width(); //wrong numbers } 

问题是我试图在函数gallery_image_load_complete()中获取加载图像的高度和宽度。 出于某种原因,这个图像的高度和宽度是关闭的,这是图像没有完全加载的情况。

有人可以帮帮我吗?

需要在load事件处理程序中调用gallery_image_load_complete():

 function load_image(image_href) { var img = new Image(); $(img).load(function () { $(this).hide(); $('#a_box').append(this); $(this).fadeIn(); gallery_image_load_complete(); // now inside the load event handler }).error(function () { }).attr('src', image_href); } 

你知道高度和宽度属性告诉你DOM中图像对象的大小,而不是底层的图像文件本身,对吧?

我现在没有方便的代码,但是当我需要任意图像的大小来决定如何呈现它时,我预先加载一个隐藏元素并获得元素的大小@ loaded。 然后按照自己的意愿行事,将图像放在需要的地方。

效果很好。