我想使用jquery检查是否存在镜像。
例如,我如何检查此图像是否存在
http://www.google.com/images/srpr/nav_logo14.png
检查结果必须是200或状态ok
var imgsrc = $(this).attr('src');
var imgcheck = imgsrc.width;
if (imgcheck==0) {
alert("You have a zero size image");
} else { //do rest of code }
谢谢,吉恩
发布于 2012-08-02 18:04:42
$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error: function(){
//do something depressing
},
success: function(){
//do something cheerful :)
}
});
来自:http://www.ambitionlab.com/how-to-check-if-a-file-exists-using-jquery-2010-01-06
发布于 2012-02-15 07:05:59
如果它不存在,则加载默认图像或处理错误
$('img[id$=imgurl]').load(imgurl, function(response, status, xhr) {
if (status == "error")
$(this).attr('src', 'images/DEFAULT.JPG');
else
$(this).attr('src', imgurl);
});
发布于 2010-08-01 18:40:16
来自here
// when the DOM is ready
$(function () {
var img = new Image();
// wrap our new image in jQuery, then:
$(img)
// once the image has loaded, execute this code
.load(function () {
// set the image hidden by default
$(this).hide();
// with the holding div #loader, apply:
$('#loader')
// remove the loading class (so no background spinner),
.removeClass('loading')
// then insert our image
.append(this);
// fade our image in to create a nice effect
$(this).fadeIn();
})
// if there was an error loading the image, react accordingly
.error(function () {
// notify the user that the image could not be loaded
})
// *finally*, set the src attribute of the new image to our image
.attr('src', 'images/headshot.jpg');
});
https://stackoverflow.com/questions/3381663
复制相似问题