jquery 判断图片是否存在

方法一、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
    }
}

  方法二、 使用ajax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 根据图片路径 判断图片是否存在
 * @param {*} url
 * @returns
 */
function hasImg(url){
    var isSuccess=true;
    $.ajax(url, {
        type: 'get',
        timeout: 1000,
        async : false,
        success: function() {
            isSuccess=true;
            console.log('图片请求成功');
        },
        error: function() {
            isSuccess=false;
            console.error('图片请求失败');
        }
    });
    return isSuccess;
}<br><br>// 使用:<br><br>
hasImg(url)

  参考地址: https://blog.csdn.net/liuyun0908/article/details/102794252

posted @   蓝色精灵jah  阅读(289)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示