首页 > 其他 > 详细

文本复制到粘贴板

时间:2020-07-03 10:14:31      阅读:50      评论:0      收藏:0      [点我收藏+]
//拷贝电话号码
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!