![损坏图像检测](https://i-blog.csdnimg.cn/blog_migrate/c4e8f79ea1efa4891e98a9c6c9f6b46f.png)
损坏图像检测
I know, you don't have broken images on your site, it's unprofessional and ugly. But sometimes you may be loading images that you don't control and you never know what's going on on the other server you're expecting to serve, but it may not feel up to the task.
我知道,您的网站上没有损坏的图像,这是不专业且丑陋的。 但是有时您可能正在加载不受控制的图像,并且您永远不知道要提供服务的另一台服务器上正在发生什么,但这可能无法胜任该任务。
One nice and simple strategy to deal with this uncertainty is to hide the images that fail to load. Browsers sent an "error" event when the worst happens and an image fails for whatever reason. Subscribe to this event using your favorite event-listener-attaching approach or library and hide the image.
解决这种不确定性的一种好方法是隐藏无法加载的图像。 当最坏的情况发生且图像由于任何原因而失败时,浏览器都会发送“错误”事件。 使用您喜欢的事件侦听器附加方法或库订阅此事件,然后隐藏图像。
Or with some ugly old-school inline event handler:
或使用一些丑陋的老式内联事件处理程序:
<img
src="broken.png"
onerror="this.style.display='none'"
/>
Simple, eh?
简单吧?
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
损坏图像检测