如何使用jQuery删除将来损坏的图像 [英] How to remove future broken image using jquery
本文介绍了如何使用jQuery删除将来损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常会正常工作..
$("img").error(function(){$(this).hide();});
但是在现场直播时不起作用
$("img").live("error",function(){$(this).hide();});
问题在于,对于那些生成了ajax的图像,我无法隐藏损坏的图像.
解决方案
您可以在将图像添加到DOM时添加事件处理程序:
$.get(urlHere, function(htmlData) {
var output = $(htmlData).find('img').error(function () {$(this).hide();}).end();
$(<selector>).html(output);
});
这是一个演示: http://jsfiddle.net/3nXcS/2/ >
更新
当您console.log()
e.bubbles
变量时,它将返回false
.因此,不能使用需要冒泡的绑定方法(.delegate()
,.live()
).
will normally, this will work..
$("img").error(function(){$(this).hide();});
but it doesnt work when putting a live on it
$("img").live("error",function(){$(this).hide();});
the problem is that, for those image which are ajax generated, i cannot hide the broken image.
解决方案
You could add the event handler as you add the images to the DOM:
$.get(urlHere, function(htmlData) {
var output = $(htmlData).find('img').error(function () {$(this).hide();}).end();
$(<selector>).html(output);
});
Here is a demo: http://jsfiddle.net/3nXcS/2/
Update
When you console.log()
the e.bubbles
variable it returns false
. So you can't use a binding method that requires bubbling (.delegate()
, .live()
).
这篇关于如何使用jQuery删除将来损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文