一般瀏覽器會報$().on()或者$().bind() is not a function的錯誤
後來上網查了查, 發現這個代碼蠻好用的, 測試通過
$("img").each(function () { let err = !1; if (!this.complete) { err = true; } if (typeof this.naturalWidth != 'undefined' && this.naturalWidth == 0) { err = true; } if (err) { $(this).bind('error.replaceSrc', function () { this.src = '/assets/img/img15.gif'; $(this).unbind('error.replaceSrc'); }).trigger('load'); } });
這裡面的jquery的unbind()可以防止圖片出錯恢複之後一閃一閃的不斷的被bind執行下去。