Possible Duplicate:
jQuery callback on image load (even when the image is cached)可能的重复:图像加载上的jQuery回调(即使在缓存图像时)
Is there a way to check if image is loaded by jquery? I have some images with external src and sometime src points to 404 page. Is there a way to check if that image is loaded? and then I can remove it from dom otherwise.
是否有办法检查图像是否被jquery加载?我有一些带有外部src的图像,有时候src指向404页面。是否有办法检查图像是否已加载?然后我可以从dom中删除它。
Thanks.
谢谢。
3 个解决方案
#1
6
jQuery has a function to deal with this, take a look at .error()
jQuery有一个函数来处理这个问题,请看。error()
So for example you could attach an .error()
handler to all images and display something else if there is an error, like the source no longer exists:
例如,您可以将.error()处理程序附加到所有图像,并在出现错误时显示其他内容,比如源不再存在:
$('img').error(function() {
$(this).hide();
}).attr("src", "missing.jpg");
这是一个演示
#2
6
@Scoobler has the right idea, but the wrong implementation.
@Scoobler的想法是正确的,但实施却是错误的。
$(function ()
{
$('img').error(function()
{
$(this).attr('src', 'http://i.stack.imgur.com/THJzu.gif');
});
});
The .error()
binding must take place on document ready - window load is too late.
.error()绑定必须在文档准备就绪时执行——窗口加载太晚了。
Demo: http://jsfiddle.net/mattball/EebmC/
演示:http://jsfiddle.net/mattball/EebmC/
#3
0
yes you can use error
event like this....
是的,你可以使用这样的错误事件....
$('#imageId')
.error(function() {
alert('Something bad happened.')
})
.attr("src", "image.gif");
#1
6
jQuery has a function to deal with this, take a look at .error()
jQuery有一个函数来处理这个问题,请看。error()
So for example you could attach an .error()
handler to all images and display something else if there is an error, like the source no longer exists:
例如,您可以将.error()处理程序附加到所有图像,并在出现错误时显示其他内容,比如源不再存在:
$('img').error(function() {
$(this).hide();
}).attr("src", "missing.jpg");
这是一个演示
#2
6
@Scoobler has the right idea, but the wrong implementation.
@Scoobler的想法是正确的,但实施却是错误的。
$(function ()
{
$('img').error(function()
{
$(this).attr('src', 'http://i.stack.imgur.com/THJzu.gif');
});
});
The .error()
binding must take place on document ready - window load is too late.
.error()绑定必须在文档准备就绪时执行——窗口加载太晚了。
Demo: http://jsfiddle.net/mattball/EebmC/
演示:http://jsfiddle.net/mattball/EebmC/
#3
0
yes you can use error
event like this....
是的,你可以使用这样的错误事件....
$('#imageId')
.error(function() {
alert('Something bad happened.')
})
.attr("src", "image.gif");