微信打開網址添加在瀏覽器中打開提示 和 微信掃描打開APP下載連接提示代碼優化,儘管已經作得很簡單,但發現不少問這類問題的都是小白。javascript
其實原來很簡單,就是判斷當前是在微信內置瀏覽器中,而後將默認隱藏的提示層顯示出來。css
第一步:判斷微信的UA。html
var ua = navigator.userAgent;java
var isWeixin = !!/MicroMessenger/i.test(ua);css3
第二步:引入默認隱藏層。瀏覽器
<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);
以上代碼,你不再用擔憂有多個按鈕了。this