如何判断图像是否在html img标签中加载失败?

时间:2013-02-08 07:22:32

标签: jquery html image

如何判断图像是否在html img标记中加载失败(404未找到,或其他4xx或5xx错误)并通过jquery回调报告?

1 个答案:

答案 0 :(得分:3)

$('img').error(function(){
    //...
});

Docs

来自文档的示例:

<img alt="Book" id="book" />

$('#book')
  .error(function() {
    alert('Handler for .error() called.')
  })
  .attr("src", "missing.png");