如何用新图像替换onerror SRC图像

how to replace onerror src image with new image

本文关键字:图像 SRC onerror 替换 何用新      更新时间:2023-09-26

不知道为什么,我的照片。Onerror图像没有加载,因为我TAB通过图像,遇到一个没有一个设置,Onerror图像仍然不加载,看到这有什么问题吗?

  if (document.getElementById("draft_player_photo")) {
     var photo = document.getElementById("draft_player_photo");
     photo.src = "http://www.myfantasyleague.com/player_photos_2013/" + pid + "_thumb.jpg";
     photo.alt = playerDatabase['pid_' + pid].name;
     photo.title = playerDatabase['pid_' + pid].name;
     photo.onerror = "this.onerror=null;this.src='http://www.myfantasyleague.com/player_photos_2010/no_photo_available.jpg';";
  }

您需要将photo.onerror定义为一个函数,就像您现在所做的那样,您只需为onerror分配一个字符串:

..
photo.onerror = function() {
    this.src=this.getAttribute('fallback') //reuse of my original idea, but whatever works
}   
...

演示-> http://jsfiddle.net/zpLm0e7d/


我将实现一个回退src:

<img id="draft_player_photo" fallback="http://www.w3schools.com/images/colorpicker.gif"src="http://www.myfantasyleague.com/player_photos_2013/11657_thumb.jpg" alt="this image">

,然后捕获任何onerror并将src设置为该回退值:

$('img').on('error', function() {
  $(this).attr('src', $(this).attr('fallback'))
});   

演示-> http://jsfiddle.net/4p8gdppL/

抱歉w3愚的参考,他们有短长度的图像。