我想使用jquery判断图像是否存在.

例如,如何判断此图像是否存在

http://www.google.com/images/srpr/nav_logo14.png 

支票必须给我一个200或ok的状态

--------------edited-------------------

var imgsrc = $(this).attr('src');
var imgcheck = imgsrc.width;


if (imgcheck==0) {
alert("You have a zero size image");
} else { //do rest of code }

Thanks Jean

推荐答案

Use the error handler like this:

$('#image_id').error(function() {
  alert('Image does not exist !!');
});

如果无法加载图像(例如,因为它不在提供的URL中),将显示alert :

Update:

我认为使用:

$.ajax({url:'somefile.dat',type:'HEAD',error:do_something});

足够判断404了.

More Readings:

Update 2:

您的代码应该如下所示:

$(this).error(function() {
  alert('Image does not exist !!');
});

No need for these lines and that won't check if the remote file exists anyway:

var imgcheck = imgsrc.width;    

if (imgcheck==0) {
  alert("You have a zero size image");
} else { 
  //execute the rest of code here 
}

Jquery相关问答推荐

从克隆的输入中读取数据属性,返回初始输入的值

如何用 jQuery / AJAX 替换表格的行

Visual Studio - 将文本粘贴到 cshtml 中会删除文本

Javascript 仅打印 iframe 内容

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

Jquery .on('scroll')在滚动时不触发事件

对混合的字母/数字数组进行排序

如何使用 jQuery 显示忙碌指示器?

JQuery html() 与 innerHTML

使用jquery设置di​​v标签的值

页面重新加载后,如何使用 twitter bootstrap 保持当前选项卡处于活动状态?

jQuery: Select 所有具有自定义属性的元素

如何在 JavaScript 中的对象数组中查找值?

JQuery - 按值查找单选按钮

jQuery从字符串中删除'-'字符

如何使用 jQuery 格式化电话号码

在jQuery中获取CSS规则的百分比值

jQuery .each() 索引?

将多个参数传递给 jQuery ajax 调用

如何使用 jQuery Select 第一个父 DIV?