且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

完整图片加载后的JavaScript执行功能

更新时间:2023-12-04 22:50:34

您可以在 .load() 事件处理程序在完全加载后触发,如下所示:

You can get the width in the .load() event handler which fires after it's fully loaded, like this:

$("#hero_image img").load(function() {
  alert($(this).width());
}).attr('src', src);

如果您要这样做并重新使用图片,请将 .load() 处理程序一次,您需要不同的行为每次使用 .one() ,因此它不会保留 em> onload 事件处理程序:

If you're doing this and re-using the image, either bind the .load() handler once, of you need different behavior each time use .one() so it doesn't keep adding an onload event handler:

$("#hero_image img").one('load', function() {
  alert($(this).width());
}).attr('src', src);