图片加载失败处理

 
//图片加载失败处理
		    jQuery('img').each(function(){
		        var error = false;
		        if (!this.complete) {//加载未成功
		            error = true;
		        }

		        if (typeof this.naturalWidth != "undefined" && this.naturalWidth == 0) {//判断图片原始尺寸失败
		            error = true;
		        }

		        if(error){//出错处理
		            $(this).bind('error.replaceSrc',function(){
		                this.src = "http://file.do.yy.com/group1/M02/01/A9/tz0MDVIghNCAaMnsAAAVHpUv0s4430.png";

		                $(this).unbind('error.replaceSrc');
		            }).trigger('load');
		        }
		    });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在OpenLayers中,当图片加载失败时,可以通过修改tileLoadFunction来处理。可以在tileLoadFunction中使用XMLHttpRequest来请求图片的状态码,根据状态码进行相应的处理。例如,当状态码为401时,可以替换图片为本地链接的某幅图片;当状态码为404时,可以替换图片为指定的404图片。以下是一个示例代码: ```javascript let wmtsLayer = new ol.layer.Tile({ source: new ol.source.WMTS({ url: url, projection: projection, tileGrid: tileGrid, format: "image/png", crossOrigin: 'anonymous', tileLoadFunction: function (imageTile, src) { return new Promise((resolve) => { let xhr = new XMLHttpRequest(); xhr.open('GET', src, true); xhr.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 200 || this.status == 304) { imageTile.getImage().src = src; } if (this.status == 401) { imageTile.getImage().src = './static/images/401.png'; } if (this.status == 404) { imageTile.getImage().src = './static/images/404.png'; } resolve(); } } xhr.send(); }); } }) }); map.addLayer(wmtsLayer); ``` 在这个示例中,我们通过XMLHttpRequest发送了一个GET请求来获取图片的状态码。根据状态码的不同,我们可以对图片进行相应的处理。当状态码为401时,将图片替换为本地链接的某幅图片;当状态码为404时,将图片替换为指定的404图片。这样可以处理图片加载失败的情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值