如何判断动态加载图像存在 [英] How to tell if dynamically loaded image exists

查看:113
本文介绍了如何判断动态加载图像存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是动态加载来自不同网站的图像到一个asp.net ListView控件是这样的:

I'm dynamically loading images from a different website into an asp.net ListView control like this:

<ListView>
   <ItemTemplate>
      <img src='<%# string.Format("http://www.Website.com/Images/{0}", Eval("ImageName")) %>' />

有时候,图像不存在的,我需要到src更改为静态路径:SRC =/不管/为Default.png。什么是我检查图像是否存在以及它(通过jQuery的任何客户端可能)不更新SRC最快的方​​法是什么? ListView控件是分页,并可能包含成千上万的记录结果集,所以我只希望检查是在当前页面上,从而优化性能图像。谢谢!

Sometimes, the images don't exist and I need to change the src to a static path: src="/whatever/default.png". What is the fastest way I check if the image exists and update the src if it doesn't (any client side possibilities via jQuery)? The ListView is paged and could contain a result set of thousands of records, so I'd only like to check the images that are on the current page to optimize performance. Thanks!

推荐答案

使用jQuery你可以这样来做:

using jquery you can do it this way:

<img src="http://myimages.com/image.jpg" id="imgId" />

$('#imgId').load(function(){
 // ... loaded  
}).error(function(){
 // ... not loaded
 $(this).attr('src','/whatever/default.png');
});

这篇关于如何判断动态加载图像存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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