複製的字符串是做爲一個值 放在input標籤裏:微信
1 <input id="wx-copy" type="text" style="position:absolute;z-index:-999;left:-1000px;top:-1000px;" value="此處爲須要複製的字符串">
開始複製input裏的值並喚起微信:app
let copyDom = document.getElementById('wx-copy') copyDom.select() copyDom.setSelectionRange(0, copyDom.value.length)
//在使用的時候input的select方法會得到焦點從而觸發了手機的鍵盤,因此在複製以後,讓input失去焦點,鍵盤就不會彈出。 setTimeout(() => { copyDom.blur() },20)
// 複製的方法:document.execCommand("copy", false, null) 這段代碼就已經把上面 select 選中的字符串給複製下來了 if(document.execCommand("copy", false, null)) { console.log('複製成功') var locatUrl = "weixin://"; //微信app地址 打開這個地址 便可喚起"微信" if (/ipad|iphone|mac/i.test(navigator.userAgent)) { var ifr = document.createElement("iframe"); ifr.src = locatUrl; ifr.style.display = "none"; document.body.appendChild(ifr); }else{ window.location.href = locatUrl; } } else { console.log('複製失敗') }