如何使用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(); $().html(output); }); 

这是一个演示: http : //jsfiddle.net/3nXcS/2/

更新

当你在console.log() e.bubbles变量时,它返回false 。 所以你不能使用需要冒泡的绑定方法( .delegate() .live() )。

$("img").live方法应该可以工作,如果没有你可以尝试livequery ( Livequery )

尝试,

 $("img").livequery('error', function(){$(this).hide();})