因为微信的限制,应用文件在内置浏览器中下载所有被屏蔽掉,形成不少人用微信扫描二维码下载时点击下载按钮没反应,我想到的是作一个提示用户在浏览器中打开下载。javascript
能够参考:微信打开网址添加在浏览器中打开提示 和 微信扫描打开APP下载连接提示代码优化。css
其实原来很简单,就是判断当前是在微信内置浏览器中,而后将默认隐藏的提示层显示出来。html
案例以下:java
1、遮罩提示:浏览器
2、微信跳转:微信自动跳转手机默认浏览器打开下载连接微信
安卓展现:直接跳转浏览器下载APK优化
苹果展现:直接跳转打开苹果商店this
部分关键代码url
第一步:判断微信的UA。spa
var ua = navigator.userAgent; var isWeixin = !!/MicroMessenger/i.test(ua);
<a href="http://caibaojian.com/test.apk" id="JdownApp">点击下载APP</a> <a href="http://caibaojian.com/test.apk" id="JdownApp2" class="btn-warn">点击下载APP2</a> <div class="wxtip" id="JweixinTip"> <span class="wxtip-icon"></span> <p class="wxtip-txt">点击右上角<br/>选择在浏览器中打开</p> </div>
第三步:添加css样式
.wxtip{background: rgba(0,0,0,0.8); text-align: center; position: fixed; left:0; top: 0; width: 100%; height: 100%; z-index: 998; display: none;} .wxtip-icon{width: 52px; height: 67px; background: url(weixin-tip.png) no-repeat; display: block; position: absolute; right: 20px; top: 20px;} .wxtip-txt{margin-top: 107px; color: #fff; font-size: 16px; line-height: 1.5;}
function weixinTip(ele){ var ua = navigator.userAgent; var isWeixin = !!/MicroMessenger/i.test(ua); if(isWeixin){ ele.onclick=function(e){ window.event? window.event.returnValue = false : e.preventDefault(); document.getElementById('JweixinTip').style.display='block'; } document.getElementById('JweixinTip').onclick=function(){ this.style.display='none'; } } } var btn1 = document.getElementById('JdownApp');//下载一 weixinTip(btn1); var btn2 = document.getElementById('JdownApp2'); //下载二 weixinTip(btn2);
以上代码,你不再用担忧有多个按钮了。