求解释一下Jquery中图片预载入插件的原理

代码如下,谢谢<script type="text/javascript">/*the images preload plugin*/(function($) {$.fn.preload = function(options) {var opts = $.extend({}, $.fn.preload.defaults, options),o= $.meta ? $.extend({}, opts, this.data()) : opts;return this.each(function() {var $e= $(this),t= $e.attr(✀rel✀),i= $e.attr(✀href✀),l= 0;$(✀<img/>✀).load(function(i){++l;if(l==2) o.onComplete();}).attr(✀src✀,i);$(✀<img/>✀).load(function(i){++l;if(l==2) o.onComplete();}).attr(✀src✀,t);});};$.fn.preload.defaults = {onComplete: function(){return false;}};})(jQuery);</script>
2026年09月25日 03:45
有1个网友回答
网友(1):

对上面代码的解释:
取结点的 rel 和 href 属性作为要预加载的图片源
在内存里分别创建 src 为以上两值的 Image 对象
等两个属性对应的图片都加载完毕之后 会执行onComplete 方法