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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库