在 jQuery 中的图像对象上绑定 onload 事件
我的网站上有以下代码:
backgroundImages[bg_img_path_b]=new Image();
backgroundImages[bg_img_path_b].src = bg_img_path_b;
backgroundImages[bg_img_path_b].loaded="loading";
//jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
if(typeof callback=='function'){
callback.call(this, bg_img_path_b);
if(showLoading) hideLoadingDC();
}
}).bind('load.cache', function(){
backgroundImages[bg_img_path_b].loaded="true";
});;
有一个大型图像库,用于用作页面包装器的背景图像。由于速度原因,我需要预加载图像(图像很大)。所以我有这段代码(实际上只是更大函数的一部分,它包装了缓存等,但是当图像不在缓存中时会触发这几行)。
backgroundImages
是 Image 对象的大数组,关键是路径是图像的路径。每个 Image 对象都有我的属性“loaded”,它表示图像是否已经加载,或者当前处于加载状态。
正如你从我的代码中看到的,我在加载图像时调用回调函数(背景发生变化等)
但是我在 IE<9 上有问题,回调没有成功启动(但不是每次) ..当我第一次加载页面时,它会正确加载,但是每当我再次调用此函数时,它都会正确地通过,没有错误,但加载事件不会触发。.
我真的不知道哪里可能出现错误,在除旧版 IE 之外的所有浏览器中,它都可以正常工作。
实际上我需要调试事件是否正确绑定,但我在 IE 和 Chrome 中的调试器加载项下都看不到它:(
请帮助我完全搞砸了,真的不知道该怎么办。
I have following code on my site:
backgroundImages[bg_img_path_b]=new Image();
backgroundImages[bg_img_path_b].src = bg_img_path_b;
backgroundImages[bg_img_path_b].loaded="loading";
//jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){
if(typeof callback=='function'){
callback.call(this, bg_img_path_b);
if(showLoading) hideLoadingDC();
}
}).bind('load.cache', function(){
backgroundImages[bg_img_path_b].loaded="true";
});;
There is large gallery for images used as background-images of page wrapper. I need to preload images because of speed (the images are quite large). So I have this code (actually is only a part of bigger function, which wraps caching and so on, but this few lines are fired when image is not in cache).
backgroundImages
is large array of Image objects, key is the path is the path of image. Every Image object has my property "loaded" which says if image has already been loaded, or is currently in state of loading.
As you can see from my code I am calling callback function when the image is loaded (there are changes of the background etc.)
But I have a problem with I.E<9, the callback is not successfully fired up (but not every time)..When I load my page for first it loads properly, but anytime I call this function again, it goes correctly through without errors, but the load event doesn't fired..
I really don't know where could be an error, in all browsers except older IEs it works fine..
Actually I need to debug if the event is bound correctly, but I can't see it in both IE and Chrome under load item in debugger:(
Please help I am completely screwed, really don't know what to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几天前,我终于找到了解决问题的方法。似乎我是先绑定onload事件,然后设置Image的url,还是先设置url,然后绑定事件,这很重要。
示例
第一个变体通常运行正常,但有时会失败(在较旧的 IE 中)..但第二个变体肯定可以正常工作..
So few days ago I finally found out a solution to my problem..It seems its matter if I at first bind the onload event and then set the url of Image, or at first set the url and then bind the event..
Example
First variant runs usually ok, but sometimes it fails (in older IEs)..But the second variant is sure working propely..
您使用的是哪个 jQuery API 版本?尝试使用最新版本的 jQuery。
否则,请使用以下简单的 JavaScript 代码在 body onload 上调用函数时加载图像:
which jQuery API version are you using? Try using the latest version of jQuery.
Otherwise use this simple JavaScript code to load images on calling your function on body onload: