我有这样的事情:
并且js:
$('a.screenThumbLink').click(function(){ console.log( $('#container').height() ); $('img.screenFirstImg').attr('src','new src'); console.log( $('#container').height() ); });
基本上单击screenFirstImage
会更改screenFirstImage
(使用更大的图像)。 但是console.log显示了完全相同的高度数,并且这是不可能的,因为在我设置新的src之后容器变大了。
也许这就是因为当我做第二个console.log时,图像还没有加载。
加载img后如何获得.height?
$("#container img").load(function () { console.log( $("#container").height() ); }); $('a.screenThumbLink').click(function(){ $('img.screenFirstImg').attr('src','new src'); });
将您的代码更改为
$('a.screenThumbLink').click(function(){ console.log( $('#container').height() ); $('img.screenFirstImg') .one('load', function(){ console.log( $('#container').height() ); }) .attr('src','new src'); });
将onload事件附加到图像,然后在图像加载时触发,然后您可以执行任何操作。
以上就是jQuery教程分享为什么更改图像的src是异步的?相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/541255.html