圖片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'));
});
}
最後更新:2017-04-03 16:48:50