如何在JQuery中检测错误加载图像? [英] How detect in JQuery error load image?

查看:54
本文介绍了如何在JQuery中检测错误加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JQuery中检测没有加载图像?然后显示而不是(链接1 显示div id =错误"

How detect no load image in JQuery? And show instead of (link 1 show div id="error"

<img src="img.pl/ax.png" />
<img src="q.pl/ph.png"/>
<div id="error" style="">
  <div id="icoka"><i class="image icon"></i></div>
  <div id="content-text-error">Sorry, image not found : (</div>
  <div style="clear:both;"></div>
</div>

推荐答案

您可以简单地使用img元素的onerror属性

you could simply use the onerror attribute of img element

<img src="noimage123.jpg" onerror="document.getElementById('error').style.display = 'block'"/>

查看示例: https://jsfiddle.net/s3du7jao/

添加了jQuery示例$("img").on("error", function(){...});

Added jQuery example $("img").on("error", function(){...});

https://jsfiddle.net/s3du7jao/2/

这篇关于如何在JQuery中检测错误加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆