本文介绍了< img>的onerror属性。正在执行javascript以更改图像。但它没有设置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。 。

this is code.. the problem is that... it is not working in Chrome and Mozilla.. but works in IE.

这是预览的链接...

this is the link of preview...http://joinqnanza.co/business.directory/index.php?cat=72

推荐答案

这样工作:

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

在线演示

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

As Nikola pointed out in the comment below, in case the backup URL is invalid as well, some browsers will trigger the "error" event again which will result in an infinite loop. We can guard against this by simply nullifying the "error" handler via this.onerror=null;.

这篇关于&lt; img&gt;的onerror属性。正在执行javascript以更改图像。但它没有设置onerror映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:36