html – 为什么Chrome的img元素的onerror事件只触发一次?

html – 为什么Chrome的img元素的onerror事件只触发一次?,第1张

概述当所有其他浏览器(IE7,8,9,FF,Opera和Safari)都重复调用它时,为什么Chrome只调用img元素的onerror事件? 有没有办法强制它再次重复攻击(在Chrome中)? jsfiddle HTML: <div id="thisWorks"> this works in Chrome. onerror event is called once. <img src 当所有其他浏览器(ie7,8,9,FF,Opera和Safari)都重复调用它时,为什么Chrome只调用img元素的onerror事件?

有没有办法强制它再次重复攻击(在Chrome中)?

jsfiddle

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.关于Chrome由于initial fiddle示例中www.asdfjklasdfasdf.com域名无效而未发布新查询的评论,我继续更改域名以匹配成功图片的域名,但是不同的文件名所以它仍然是404.这将证明它不是无效的域名导致Chrome在第二次尝试时纾困.

编辑:
更新了小提琴,并将jquery的使用简单地删除,并将其排除在外.

@R_301_6120@ 我尝试过上面提到的方法,setAttribute(‘src’,null)有副作用,即添加另一个请求.

最后,我用

setTimeout(function(){ imgElement.src = 'http://xxxx'; },0)

,这个有效!

例如,请参见jsfiddle.

总结

以上是内存溢出为你收集整理的html – 为什么Chrome的img元素的onerror事件只触发一次?全部内容,希望文章能够帮你解决html – 为什么Chrome的img元素的onerror事件只触发一次?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1106696.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存