如何使用img元素的onerror属性

时间:2011-11-14 16:39:00

标签: html css image onerror

CSS:

.posting-logo-div {  }
.posting-logo-img { height:120px; width:120px; }
.posting-photo-div { height:5px;width:5px;position:relative;top:-140px;left:648px; }
.posting-photo-img { height:240px; width:240px; }

HTML:

<div id="image" class="posting-logo-div"><img src="../images/some-logo1.jpg" onerror="this.src='../images/no-logo-120.jpg';" class="posting-logo-img"></div>
<div id="photo" class="posting-photo-div"><img src="../images/some-logo2.jpg" onerror="this.src='../images/no-logo-240.jpg';" class="posting-photo-img"></div>

这似乎不适用于Chrome或Mozilla,但可以在IE中使用。

4 个答案:

答案 0 :(得分:96)

这有效:

<img src="invalid_link"
     onerror="this.onerror=null;this.src='https://placeimg.com/200/300/animals';"
>

现场演示: http://jsfiddle.net/oLqfxjoz/

正如Nikola在下面的评论中所指出的,如果备份URL也是无效的,一些浏览器将再次触发“错误”事件,这将导致无限循环。我们可以通过this.onerror=null;简单地取消“错误”处理程序来防范这种情况。

答案 1 :(得分:1)

这实际上非常棘手,特别是如果您打算为需要使用onerror条件图片网址连接字符串的用例返回图片网址,例如您可能希望以编程方式在CSS中设置url参数。

诀窍是图像加载本质上是异步的,因此onerror不会同步发生,即如果你调用returnPhotoURL它会立即返回undefined bcs异步加载/的方法刚刚开始处理图像负载。

所以,你真的需要将你的脚本包装在Promise中然后像下面那样调用它。注意:我的示例脚本执行其他一些操作,但显示了一般概念:

returnPhotoURL().then(function(value){
    doc.getElementById("account-section-image").style.backgroundImage = "url('" + value + "')";
}); 


function returnPhotoURL(){
    return new Promise(function(resolve, reject){
        var img = new Image();
        //if the user does not have a photoURL let's try and get one from gravatar
        if (!firebase.auth().currentUser.photoURL) {
            //first we have to see if user han an email
            if(firebase.auth().currentUser.email){
                //set sign-in-button background image to gravatar url
                img.addEventListener('load', function() {
                    resolve (getGravatar(firebase.auth().currentUser.email, 48));
                }, false);
                img.addEventListener('error', function() {
                    resolve ('//rack.pub/media/fallbackImage.png');
                }, false);            
                img.src = getGravatar(firebase.auth().currentUser.email, 48);
            } else {
                resolve ('//rack.pub/media/fallbackImage.png');
            }
        } else {
            img.addEventListener('load', function() {
                resolve (firebase.auth().currentUser.photoURL);
            }, false);
            img.addEventListener('error', function() {
                resolve ('https://rack.pub/media/fallbackImage.png');
            }, false);      
            img.src = firebase.auth().currentUser.photoURL;
        }
    });
}

答案 2 :(得分:0)

非常简单

  <img onload="loaded(this, 'success')" onerror="error(this, 
 'error')"  src="someurl"  alt="" />

 function loaded(_this, status){
   console.log(_this, status)
  // do your work in load
 }
 function error(_this, status){
  console.log(_this, status)
  // do your work in error
  }

答案 3 :(得分:0)

<块引用>

将 product.invoice_url 替换为您的动态网址

<img onerror="this.onerror=null;this.src='https://upload.wikimedia.org/wikipedia/commons/1/14/No_Image_Available.jpg';" [src]="product.invoice_url" class="img-thump" >

css

  .img-thump{
    height: 120px;
    width: 170px;
  }
相关问题