`
380071587
  • 浏览: 450005 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

图片onerror事件,为图片加载指定默认图片

 
阅读更多
为图片指定加载失败时显示默认图片,js输出的img对象,onerror是事件,不是属性,所以这样写是不起作用的:
var img = $(document.createElement("IMG"));
img.attr({
"src": imgs[idx],
"alt": tips[idx],
"onerror":"this.src='" + NoPicPath + "'"
}).appendTo(div);

应该是绑定事件:
//图片加载失败时,加载默认图片
$('img').error(function () {
$(this).attr('src', NoPicPath);
});
if ($.browser.msie && $.browser.version < 9) {
$('img').each(function () {
$(this).attr('src', $(this).attr('src'));
});
}

参考:http://www.paulund.co.uk/handle-image-loading-errors-with-jquery
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics