在缓存图像上触发 load() 事件的更好方法是什么?
我正在编写一个脚本,该脚本等待内容加载到隐藏的 div
中,然后再激活指向它的缩略图。
$('#preload img:first-child') .bind('加载',activateThumb) .each(函数(){ if(this.complete || this.complete===未定义) $(this).load()});
each
部分会触发缓存中图像的 load()
事件。我必须添加它才能使页面在某些不会在缓存图像上触发 load()
的浏览器中工作。
还有一个插件本质上通过触发加载事件而不是“手动”来完成相同的事情但通过重置 src 属性。
从编程的角度来看,哪个是更优雅的解决方案?
I'm working on a script that waits for content to load in a hidden div
before activating a thumbnail that points to it.
$('#preload img:first-child') .bind('load',activateThumb) .each(function(){ if(this.complete || this.complete===undefined) $(this).load()});
The each
part triggers the load()
event for images in the cache. I had to add it in order to make the page work in some browsers that don't fire load()
on cached images.
There is also a plugin here that does the same thing essentially, by triggering the load event not "manually" but by resetting the src
attribute.
From a programming standpoint, which is the more graceful solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重置 src 属性来触发 load 处理程序似乎有点像黑客(根据我的知识)。除非插件的作者有特殊原因这样做,或者知道我们不知道的事情,否则我会坚持使用您手动触发
load
的方法。Resetting the
src
attribute to fire theload
handler seems like a bit of a hack (based on my knowledge). Unless the author of the plugin has some special reason to do that, or knows something we don't, I would stick with your method of manually triggeringload
.提到的插件event.special.load在 jquery load-event 文档中,以类似的方式解决了这个问题。
The plugin event.special.load, which is mentioned in the jquery load-event documentation, solves that in a similar way.