javascript - 图片加载失败时显示 fa-icon

标签 javascript php jquery html

我想在图像加载失败时显示 fa 图标。就像在 alt 属性文本中出现一样,如果图像加载失败,我想显示一个 fa-icon,如果有任何方法使用 js/jquery 等,请分享。 我正在使用的代码:

<div class="panel-body text-center" style="background-color:#f99d1c;">
                        <?php if($count>0){
                            echo "<img src='/presenations/$company/$curl/$fileurl' class='img-thumbnail' width='165px' class='img-thumbnail' alt='ERR_RLF'>";
                        }else{
                            echo "<i class='fa fa-video-camera fa-4x' style='color:#FFF;'></i>";
                        }?>
                        </div>

enter image description here

最佳答案

我认为您正在寻找 img 元素的 onerror 属性。

这是:

已更新

纯JS解决方案

function imgError(image) {
    image.outerHTML= '<i class="fa fa-video-camera fa-4x"></i>';
    return true;
}
.red_block {
  background: red;
  padding: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>


<!-- Image element with invalid path so it does not load and onerror gets call -->
<img src="image.png" onerror="imgError(this);"/>

<br>
<!-- Same thing with valid image path so it loads -->
<img src="http://via.placeholder.com/350x150" onerror="imgError(this);"/>

So what you need to replace here is HTML structure of fonts if you want anything else than this.

关于javascript - 图片加载失败时显示 fa-icon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49624266/

相关文章:

javascript - JQuery Animate 没有动画

javascript - 可拖动 div 来控制溢出 div

php - 使用 codeigniter 将今天的日期与 mysql 表中的日期进行比较

jquery - YouTube 播放按钮在 iPhone 上消失

javascript - jQuery .on() 悬停不适用于 HTML5 选择框

javascript - 获取 [object HTMLLIElement] 而不是文本。 Javascript函数

javascript - 无法验证 json_decode 关联数组元素

php - 修改将数据插入表中,使其现在为更新表

javascript - Google map v3 在 jQuery mobile 和 PhoneGap 上具有自动完成功能

javascript - 尝试使用 JS 在单击事件上应用类