jQuery的:检查是否存在图像 [英] jQuery: Check if image exists

查看:122
本文介绍了jQuery的:检查是否存在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载通过jQuery $。阿贾克斯的图像路径​​和显示图像之前,我想检查它是否在事实上存在。我可以使用图像加载/ ready事件或类似的东西来确定该文件的路径是否有效?

I'm loading an image path via jQuery $.ajax and before showing the image I'd like to check if it in fact exists. Can I use the image load/ready event or something similar to determine that the file path is valid?

已经.myimage设置为显示:没有,我希望能像做

Having .myimage set to display: none, I'm hoping to do something like

$(".myimage").attr("src", imagePath);
$(".myimage").load(function() {
    $(this).show();
});

就是这样有可能什么?

Is anything like that possible?

推荐答案

好了,你可以绑定 .error( ) 处理...

Well, you can bind .error() handler...

这样,

$(".myimage").error(function(){
  $(this).hide();
});

好了,你是负载情况还好已经

$(".myimage").load(function() {
    $(this).show();
});

这个问题是在JavaScript被禁用的形象永远不会显示...

the problem with this is if Javascript was disabled the image will not ever show...

这篇关于jQuery的:检查是否存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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