处理"404图像错误".jQuery/Javascript错误 [英] Handling "404 image error" error with jQuery/Javascript

查看:60
本文介绍了处理"404图像错误".jQuery/Javascript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载YouTube托管的视频的缩略图时,出现404错误是实际图像.例如: http://i.ytimg.com/vi/eMFYg2oZ4qE/default.jpg http://i.ytimg.com/vi/0/default.jpg

When loading a thumbnail of a video hosted by Youtube, a 404 error is an actual image. eg: http://i.ytimg.com/vi/eMFYg2oZ4qE/default.jpg http://i.ytimg.com/vi/0/default.jpg

由于错误显示为真实图像,所以

Since the error presents itself as a authentic image,

<img onerror="alert('error!')" src="http://i.ytimg.com/vi/0/default.jpg">

无法捕获404错误...我该如何解决此问题?

does not work in catching the 404 error... How can I tackle this problem?

推荐答案

我相信这些内容是在YouTube服务器端处理的,但是由于您使用一小组图像将其标记为jQuery,因此可以使用简单的AJAX请求,然后在失败时将"src"属性更改为默认图像路径.

I believe those are handled server-side on YouTube, but since you tagged this as jQuery, using a small group of images, you could check their existence with a simple AJAX request, then on failure, change the "src" attribute to your default image path.

$.ajax("/path/to/image.img", failure: function() {
  $('#this-image').attr('src', 'default.jpg');
})

虚拟选择器,您需要使其适合您的应用程序逻辑

Dummy selectors in that, you'd need to make it fit the logic of your application

这篇关于处理"404图像错误".jQuery/Javascript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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