有没有办法在更改它的src后绑定到图像的load()事件? [英] Is there a way to bind to the load() event of an image after changing it's src?

查看:111
本文介绍了有没有办法在更改它的src后绑定到图像的load()事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片< img id ='my_img'src ='img1.png'> 和一个按钮。
单击该按钮可执行以下操作:

I have an image <img id='my_img' src='img1.png'> and a button. Clicking the button does the following:

$('#my_img').attr('src','img2.png')

第二张图片(img2.png)相当大,我想要知道它何时满载(即.load()事件)。
我试图使用.live,.bind和.complete,但似乎没有。

This second image (img2.png) is fairly large, and I want to know when it is fully loaded (i.e. .load() event). I tried to use .live, .bind, and .complete but none seem to fire.

任何想法?

谢谢!
Amit

Thanks! Amit

推荐答案

我认为每个img标签只会触发一次load事件。但是你可以轻松地创建一个新的图像,并使用新的加载处理程序加载它。

I think the load event only fires once per img tag. But you can easily make a new image, and load that with a new load handler.

var img = new Image();
img.onload = function() {
  $('#my_img').attr('src', img.src);
};
img.src = 'img2.png';

加载后,您可以在页面上设置图像的src,它会立即从浏览器缓存。

Once it's loaded you can set the src of the image on your page and it will pull instantly from the browser cache.

这篇关于有没有办法在更改它的src后绑定到图像的load()事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆