setTimeout(function(){//做事情},0)
...这个0会让浏览器系统(DOM)再多进行一次更新,确保一切都已经正确更新。 - dsdsdsdsd.load()
不会触发事件且只有一个必需参数,请改用 .trigger("load")
。 - ForceUser我可以建议您将其重新加载到非DOM图像对象中吗?如果已缓存,这将不需要任何时间,并且仍将触发onload事件。如果未缓存,则在图像加载完成时将触发onload事件,这应该与DOM版本的图像完成加载的时间相同。
Javascript:
$(document).ready(function() {
var tmpImg = new Image() ;
tmpImg.src = $('#img').attr('src') ;
tmpImg.onload = function() {
// Run onload code.
} ;
}) ;
已更新(以处理多个图像,并具有正确排序的onload附件):
$(document).ready(function() {
var imageLoaded = function() {
// Run onload code.
}
$('#img').each(function() {
var tmpImg = new Image() ;
tmpImg.onload = imageLoaded ;
tmpImg.src = $(this).attr('src') ;
}) ;
}) ;
load
处理程序,此外这种方法只适用于一张图片,而 OP 的代码适用于多张图片 :) - Nick Craver#img
是一个 ID,而不是元素选择器 :) 此外,this.src
可以工作,不需要在不必要的情况下使用 jQuery :) 但在任何情况下创建另一个图像似乎都有些过度设计。 - Nick Craver我的简单解决方案,不需要任何外部插件,对于常见情况应该已经足够了:
/**
* Trigger a callback when the selected images are loaded:
* @param {String} selector
* @param {Function} callback
*/
var onImgLoad = function(selector, callback){
$(selector).each(function(){
if (this.complete || /*for IE 10-*/ $(this).height() > 0) {
callback.apply(this);
}
else {
$(this).on('load', function(){
callback.apply(this);
});
}
});
};
使用方法如下:
onImgLoad('img', function(){
// do stuff
});
例如,要在加载时淡入图像,您可以执行以下操作: $(document).ready(function() {
$('img').hide();
$('img').fadeIn(1000);
});
$('img').hide();
onImgLoad('img', function(){
$(this).fadeIn(700);
});
或者,如果你更喜欢像 jQuery 插件一样的方法:
/**
* Trigger a callback when 'this' image is loaded:
* @param {Function} callback
*/
(function($){
$.fn.imgLoad = function(callback) {
return this.each(function() {
if (callback) {
if (this.complete || /*for IE 10-*/ $(this).height() > 0) {
callback.apply(this);
}
else {
$(this).on('load', function(){
callback.apply(this);
});
}
}
});
};
})(jQuery);
并且以这种方式使用它:
$('img').imgLoad(function(){
// do stuff
});
例如:$('img').hide().imgLoad(function(){
$(this).fadeIn(700);
});
width
,max-height
并保留 height:auto
,通常只有在需要拉伸图像时才需要同时设置宽度和高度;对于更强大的解决方案,我建议使用像 ImagesLoaded 这样的插件。 - guari你真的需要使用jQuery吗?你也可以直接将onload
事件附加到你的图像上;
<img src="/path/to/image.jpg" onload="doStuff(this);" />
它会在图片被加载后每次触发,不论是否从缓存中获得。onload
处理程序会触发吗? - SexyBeast您还可以使用此代码,并支持加载错误:
$("img").on('load', function() {
// do stuff on success
})
.on('error', function() {
// do stuff on smth wrong (error 404, etc.)
})
.each(function() {
if(this.complete) {
$(this).load();
} else if(this.error) {
$(this).error();
}
});
load
处理程序,然后在each
函数中稍后调用它。 - GreenRaccoon23我自己也遇到了这个问题,在各处搜寻解决方案,不想清空缓存或下载插件。
由于我没有立即看到这个主题,所以我找到了其他的解决方法,这是一个有趣的修复方法,值得在这里发布:
$('.image').load(function(){
// stuff
}).attr('src', 'new_src');
实际上,我是从这里的评论中得到了这个想法:http://www.witheringtree.com/2009/05/image-load-event-binding-with-ie-using-jquery/
我不知道为什么它起作用,但我已经在IE7上测试过了,在以前会出问题的地方现在已经可以工作。
希望对你有所帮助,
被接受的答案实际上解释了原因:
如果src已经设置,则事件将在您绑定事件处理程序之前在缓存中触发。
$image.load(function(){ something(); }).attr('src', $image.attr('src'));
重置了原始源。 - Mauricevar photo = document.getElementById('image_id');
var img = new Image();
img.addEventListener('load', myFunction, false);
img.src = 'http://newimgsource.jpg';
photo.src = img.src;
$('<img />', {
"src": $("#img").attr("src")
}).load(function(){
// Do something
});
对GUS示例的修改:
$(document).ready(function() {
var tmpImg = new Image() ;
tmpImg.onload = function() {
// Run onload code.
} ;
tmpImg.src = $('#img').attr('src');
})
onload
处理程序之前没有必要设置src
,之后一次就足够了。 - Nick Craver在img对象被定义之后,单独一行添加src参数即可。这样可以欺骗IE触发load事件。虽然不太美观,但这是我目前发现的最简单的解决方法。
jQuery('<img/>', {
src: url,
id: 'whatever'
})
.load(function() {
})
.appendTo('#someelement');
$('#whatever').attr('src', url); // trigger .load on IE