处理在JQuery中
问题描述:
丢失图像jQuery的它以前可能处理使用fn.error()处理在JQuery中
var photo = $('#photo');
photo.find('.photo-wrapper')
.error(function() { $(this).attr('src' , '/images/nosuchphoto.png'); })
.attr('src', '/images/photoserver/photo.png');
图像丢失,但是这已经在JQuery的1.8被弃用。
现在1.9出来了,处理丢失图像的最佳方法是什么?
答
改为使用on("error")
。
var photo = $('#photo');
photo.find('.photo-wrapper')
.on('error', function() { $(this).attr('src' , '/images/nosuchphoto.png'); })
.attr('src', '/images/photoserver/photo.png');
答
对于可能存在的我觉得最优雅的解决方案是期运用$阿贾克斯,如图片:
$.ajax({
url: 'your_image.jpg',
type: "POST",
dataType: "image",
success: function() {
/* function if image exists (setting it in div or smthg.)*/
},
error: function(){
/* function if image doesn't exist like hideing div*/
}
});
但有些人喜欢期运用隐伏的图像,之后展示自己的负载,如:
<img src="your_image.jpg" onload="loadImage()">
两种解决方案都是有效的,使用最适合您的问题的解决方案
+1你打败了我。 (LOL)... –
谢谢...我知道这很简单... – Greg