项目中,要使用到缺省图,除了火狐浏览器,其它浏览器都支持img onerror事件。我使用到的解决的办法就是给图片的外层标签加背景,背景图用的就是缺省图。

上代码

<img src="" alt=" " class="goods-img" onerror="onerror=null;src='../themes/images/error.jpg'">
.goods-img {
    display: inline-block;
    width: 178px;
    height: 178px;
    padding: 0;
    margin-top: 5px;
    float: none;
    background: url(../images/error.jpg) no-repeat;
}

这样就可以了,把缺省图当做背景图来使用,图片正常显示的话,就会给背景图覆盖掉.

相关文章:

  • 2021-08-08
  • 2021-08-14
  • 2021-12-06
  • 2021-12-20
  • 2022-01-11
  • 2021-06-18
  • 2021-08-13
  • 2021-10-15
猜你喜欢
  • 2021-10-25
  • 2021-12-03
  • 2022-12-23
  • 2022-03-04
  • 2021-10-04
  • 2021-05-20
  • 2021-12-07
相关资源
相似解决方案