JQ无网络时用图片的base64编码给image设置src路径

前端开发   发布日期:2023年05月30日   浏览次数:1108

比如我要在一个页面实时监听与服务器是否还在心跳连线中,是否与服务器断开等情况时,在界面中增加一个美化的图片用于提示以提高友好度。而一般情况下图片引用的src图径都是从服务器加载,如果在断网前未加载到缓存时,而突然断网后又应该显示一张断网的图片时就无法从服务器加载到了,这时候就要用到图片的base64编码格式直接解析出图片,就不用担心无网络时的加载问题了,具体看代码吧:

代码中包含了如何设置图片的src路径,以及提供了两张图片的base64编码供大家参考,至于如何将图上转为base请参考PHP博客

   function check_zt(){
		var isFocus=$("#saomiaoma").is(":focus");//临时判断一下焦点是否在指定的文本框中
		if(isFocus==true){
			var img_data="data:image/gif;base64,R0lGODlhMgAKAOZ/APNumJmCu/39/fvm4/qlHJDGbOrT502Uz+wlKusXWmq9Rfnm2fXc1ajLjvKyVmZwssCDtDNQo9nU6PC5aun29rLE3YRxrrJLnPPJiu1seff31+jmE7x2rW+dy3mo1dnl8/b2xjmNy+Xi8OsdJcudwW5Too212+toTufmZOrz5MvT5/rCa/jDWqOmz4uSxHXCxpfU12ZKnMrm6Pv75GjIzvHm8XHL0M7J3/L18eze6+Px2XfBTuXjI+7mW4m+Z1xoqfrWx+fu9LPVoNvYI97aX+vAh9ru0HODu/D5+dzx8sPR44zHy+BiSPXx5+Hq9tXo6uvnkP769fPy+Pzv3cfcrKaYxOjm8pLGyfb169fVZJzRfpvRzK/Y2erv98/j8Y3Q2EFbqPnHyOzvwvHSpbVYoOHu2O4tam7Kz/z39f7oxtpdReBic+/PzvrF1pbN0eucp/3y6+6dja4+l+HgjPr66u3wz/n4+l3EzObs+OAeI9lbbd/e7vOgq/z8/P///////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFAAB/ACwAAAAAMgAKAAAHWYB+fYOEfX5+f4mKi4yNjo+LC2pMlJRqDIiQmpuQDHkjoKFAmZylpZ4jCKoII6Omr5uoq6yusLaNsqutpLe9uaq7vcJ/qKGgtcO2C0wnzc6Yybe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ebb3dHt4bCyscrz6PXf9ywxOUGwYKcJLDDMgMKwlA4dqQKy8kOxosWLGClKXBUIACH5BAkAAH8ALAAAAAAyAAoAAAeFgH59g4R9fgKIiYh+fn+Oj5CRkpOPC2pMmJhqDHAYGGMzGqJRjZSmp48MeSOsrUBwLCwYMyC1pKi4lKojCL0II6+xs7Ugt7nHqau+v8GytLalyMe7y8CwzsTG0rnUvtbCz8XR26i7razNw9DkuQtMJ/DxDHSeY3SiGtrspoz9/v//9qEKBAAh+QQFAAB/ACwAAAAAMgAKAAAHbYB+fYOEfX5+AgKHf4yNjo+QkZILakyWlmoMUQsLU36SoKGgDHkjpqdAUWlpnqKur6QjCLMII6mrra+6krG0tbesn7vDjr20tqrBxMt/xrPIuMLMu7GnpsC507oLTCfe35pT4tLarofn6OnluoEAOw==";
			$("#zt_img").attr('src',img_data);
			$("#zt_img").show();
		}else{
			var img_data="data:image/gif;base64,R0lGODlhCgAKALMIAP7+/uDg4MzMzL29vbCwsJycnI2NjXx8fP4BAgAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBwAIACwAAAAACgAKAAAELxChUM4pQaJh+xnTYRwAwB2UAYxGwFUBEFiqt1rxXA0Bu2KWQUmE2niCGooFI4kAACH5BAUHAAgALAEAAQAIAAgAAAQaEElkJiKhCklQsMiBDCBIitbXIZsRrFIFSBEAIfkEBQcACAAsAQABAAgACAAABB8QIUOOlAKMMtHJFSJIgWGQ6GQEmEeI1lkMwHgdxIlEACH5BAUHAAgALAIAAQAGAAgAAAQZEI2JkAlgGBkCEhUCVMCIHCHoDdYABJtERQAh+QQFBwAIACwCAAEABgAIAAAEFxAdhIwUCIiJCDEEII1k9nnUIYhblSIRACH5BAUHAAgALAIAAQAGAAgAAAQZEKFDihwSEIGMGIYkDpjYDUIoEMAlFcQhRQAh+QQFBwAIACwBAAEACAAIAAAEGhBJdIyZKOSBUQHApSHWIVVXRoJFN2ijVGIRACH5BAUHAAgALAEAAQAIAAgAAAQgEEl5jjQCBVEMGleAdJUkAMMAZEhlFJr3Ia8gCnI5IREAIfkEBQcACAAsAQACAAgABgAABBkQHQTAOMeYgrpAA8BNiPBJSGGVXaphIYBGADs=";

			$("#zt_img").attr('src',img_data);
			$("#zt_img").show();
		}
		
		setTimeout(function(){
			check_zt()
		},1000);
	}
	
	$(document).ready(function(){
		get_focus();
		check_zt()
	});

以上就是JQ无网络时用图片的base64编码给image设置src路径的详细内容,更多关于JQ无网络时用图片的base64编码给image设置src路径的资料请关注九品源码其它相关文章!