微信打開網址添加在瀏覽器中打開提示 和 微信掃描打開APP下載鏈接提示代碼優化,盡管已經做得很簡單,但發現很多問這類問題的都是小白。
其實原來很簡單,就是判斷當前是在微信內置瀏覽器中,然后將默認隱藏的提示層顯示出來。
第一步:判斷微信的UA。
-
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;}
第四步:點擊按鈕顯示隱藏層,點擊隱藏層關閉,總的JS代碼如下:
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);
以上代碼,你再也不用擔心有多個按鈕了。

