[英]Can't detect IMG jquery error event when using handlebars.js. How can I fix these broken images?
我無法檢測到源損壞的圖像的錯誤事件。
圖像是在這樣的車把中創建的:
<img src="{{image}}" alt="the image">
我正在嘗試以下代碼:
console.log('first log');
$('img').one('error', function() {
console.log('image error detected');
this.src = "<my other image path here>";
});
我無法獲得console.log,除非在Chrome瀏覽器的導航欄中單擊“后退”然后“前進”。 如果我先按“后退”再按“前進”,則替換“ this.src”也可以。
我已經嘗試了很多事情,例如.load或將此代碼放在文檔中等。
當查看控制台時,我在此頁面上看到兩個圖像的兩個GET,后面是上面的“第一個日志”。 因此,在綁定我的偵聽器之前,圖像碰到了錯誤事件。 或類似的東西...
有什么建議么?
終於成功了。 我敢肯定有更好的方法,但這就是為我做的:
if ($('[data-path="/blahblah"]').length) {
$('.myClass img').each(function() {
var image = $(this);
if (image.context.naturalWidth == 0 || image.readyState == 'uninitialized') {
image.unbind('error').attr("src", "<placeholder src here>");
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.