Javascript 为什么更改映像的src是异步的?

Javascript 为什么更改映像的src是异步的?,javascript,jquery,Javascript,Jquery,我有这样的东西: <div id="container"> <img id="screenFirstImg"> </div> 基本上,只要点击一个tumbnail,screenFirstImage就可以更改(使用更大的图像)。但console.log显示了完全相同的高度,这是不可能的,因为在我设置了新的src之后,容器会变大 可能是因为当我使用第二个console.log时,图像尚未加载。 加载img后如何获取.height?将onload事件附加到图像,

我有这样的东西:

<div id="container">
 <img id="screenFirstImg">
</div>
基本上,只要点击一个tumbnail,
screenFirstImage
就可以更改(使用更大的图像)。但console.log显示了完全相同的高度,这是不可能的,因为在我设置了新的src之后,容器会变大

可能是因为当我使用第二个console.log时,图像尚未加载。

加载img后如何获取.height?

将onload事件附加到图像,该事件将在加载图像时触发,然后您可以执行任何需要的操作。

将代码更改为

$('a.screenThumbLink').click(function(){

  console.log( $('#container').height() );
    $('img.screenFirstImg').attr('src','new src');
  console.log( $('#container').height() );

});
$("#container img").load(function () {
  console.log( $("#container").height() );
});

$('a.screenThumbLink').click(function(){
  $('img.screenFirstImg').attr('src','new src');
});

@是123是的。这是我们的文档。您可能把它与;-)混淆了
$('a.screenThumbLink').click(function(){

  console.log( $('#container').height() );
  $('img.screenFirstImg')
      .one('load', function(){
               console.log( $('#container').height() );
          })
      .attr('src','new src');
});