//拷贝电话号码 function copyPhone(tel){ var strHtml = ` <div class="tanbg" style="position: fixed;top:0;left:0;right:0;bottom:0;background: rgba(0, 0, 0, 0.5);z-index: 999;"> <div class="phoneCopy" style="background:#fff; width:80%; padding:20px 20px 10px; position: absolute; top:50%; left:50%; transform: translate(-50%,-50%); z-index: 1000;"> <div class="cuo" onclick="$(‘.tanbg‘).fadeOut()" style="font-size: 25px; color:#999; position: absolute; top:5px; right: 10px;">×</div> <div class="phoneTit" style="font-size:14px; color:#999; line-height: 1.5; text-align: center; padding-top:20px; ">当前浏览器不支持,请复制号码拨打</div> <div class="phone" style="font-size:16px; color:#666; line-height: 1.2; text-align: center; margin-top:20px;">${tel}</div> <div class="copyText" style="width:100px; font-size:14px; color:#fff; background:#FF5C00; border-radius: 5px; text-align: center; height: 40px; line-height: 40px; margin:20px auto; " onclick="copy()">点击复制号码</div> </div> </div> ` var u = navigator.userAgent; var isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1; //g var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 var isUc = navigator.userAgent.indexOf(‘UCBrowser‘) > -1; if (isAndroid && isUc) { // $(‘.tanbg‘).fadeIn() // $(‘.phoneCopy‘).fadeIn() $(‘body‘).append(strHtml); // $(".phone").text(tel) } else { window.location.href = "tel:" + encodeURIComponent(tel) } } function copy() { let val = document.querySelector(".phone").innerText; copyValue(val) } function copyValue(text) { // 数字没有 .length 不能执行selectText 需要转化成字符串 const textString = text.toString(); let input = document.querySelector(‘#copy-input‘); if (!input) { input = document.createElement(‘input‘); input.id = "copy-input"; input.readOnly = "readOnly"; // 防止ios聚焦触发键盘事件 input.style.position = "absolute"; input.style.left = "-1000px"; input.style.zIndex = "-1000"; document.body.appendChild(input) } input.value = textString; // ios必须先选中文字且不支持 input.select(); selectText(input, 0, textString.length); if (document.execCommand(‘copy‘)) { document.execCommand(‘copy‘); } else { console.log(‘不兼容‘); } input.blur(); // input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法 // 选择文本。createTextRange(setSelectionRange)是input方法 function selectText(textbox, startIndex, stopIndex) { if (textbox.createTextRange) { //ie const range = textbox.createTextRange(); range.collapse(true); range.moveStart(‘character‘, startIndex); //起始光标 range.moveEnd(‘character‘, stopIndex - startIndex); //结束光标 range.select(); //不兼容苹果 } else { //firefox/chrome textbox.setSelectionRange(startIndex, stopIndex); textbox.focus(); } } $("#copy-input").hide() var ele = document.querySelector(‘body‘); ele.scrollTop = ele.scrollHeight; // document.querySelector(".modelBess").style.display = ‘none‘; // alert(‘已复制‘) $(‘.tanbg‘).fadeOut() showToast(‘已复制去拨打电话‘) };
vue中
copyValue() { let url = this.tel400; let oInput = document.createElement("input"); oInput.value = url; document.body.appendChild(oInput); oInput.select(); // 选择对象; console.log(oInput.value); document.execCommand("Copy"); // 执行浏览器复制命令 this.close(); Toast(‘复制成功去拨打电话‘); oInput.remove(); },
原文:https://www.cnblogs.com/zjz666/p/13228510.html