【发布时间】:2013-07-12 06:20:33
【问题描述】:
为什么其他浏览器(IE7、8、9、FF、Opera、Safari)都重复调用img元素的onerror事件,Chrome只调用一次?
有没有办法强制它再次重复 onerror 调用(在 Chrome 中)?
HTML:
<div id="thisWorks">
this works in Chrome. onerror event is called once.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://eatfrenzy.com/images/success-tick.png" />
</div>
<div id="thisDoesNotWork">
this does not work in Chrome. onerror event is not called twice.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://www.asdfjklasdfasdf.com/bogus2.png|http://eatfrenzy.com/images/success-tick.png" />
</div>
JAVASCRIPT:
function fixit(img)
{
var arrPhotos = img.getAttribute('rsrc').split('|');
// change the img src to the next available
img.setAttribute('src', arrPhotos.shift());
// now put back the image list (with one less) into the rsrc attr
img.setAttribute('rsrc', arrPhotos.join('|'));
return true;
}
编辑:
根据@Sunil D.关于在initial fiddle 示例中由于www.asdfjklasdfasdf.com 的无效域名而导致Chrome 未发出新查找的评论,我继续更改域名以匹配成功图像的域名,但使用了不同的文件名,所以它仍然是 404。这将证明它不是导致 Chrome 在第二次尝试时退出的无效域名。
编辑: 更新了 fiddle 并删除了 jquery 的使用,以简化事情并将其排除在外。
【问题讨论】:
标签: html image google-chrome events onerror