在网上搜索了好长时间,有些都没有效果,后来终于实现了!需要的小伙伴们可以看下下面的代码,希望可以帮助你们~
function isHasImg( src ){
var img = new Image();
img.src = src;
img.onload = function(){
if( img.width > 0 || img.height > 0 ){
onImgExistNotify(img.src,true,3);
}
else{
onImgExistNotify(img.src,false,2);
}
}
img.onerror = function(){
onImgExistNotify(img.src,false,1);
}
}
function onImgExistNotify(src,bExist,iPlace){//图片src是否存在通知
if( bExist ){
console.log("图片src="+src+"存在"+iPlace);
}
else{
console.log("图片src="+src+"不存在"+iPlace);
}
}