在移动端实现在浏览器网页点击按钮并复制文字(且测试打开微信APP)

在IOS手机上测试了以下的浏览器、链接等:

打不开微信:百度浏览器,搜狗浏览器搜狗搜索,在钉钉点开链接

打得开微信:UC浏览器,QQ浏览器,Chrome浏览器,Safari浏览器,360浏览器,Firefox浏览器,e浏览器(绿色版),傲游浏览器,R浏览器,浏览器+

 

html:

<span style="display:none;" id="linkCopy">复制的内容</span>
<p class="copy" id="btnWechat" style="display:inline-block;padding:3px 8px;border: 1px solid #333;" >点我</p>

script:

 <script>
    // 浏览器判断
    var browser = {
       versions: function() {
          var u = navigator.userAgent;
          return {
             trident: u.indexOf('Trident') > -1,    // IE内核
             presto: u.indexOf('Presto') > -1,      // opera内核
             webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
             gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, // 火狐内核
             mobile: !!u.match(/AppleWebKit.*Mobile.*/),            // 移动终端
             ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),      // ios终端
             android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或者uc浏览器
             iPhone: u.indexOf('iPhone') > -1,  // iPhone或者QQHD浏览器
             iPad: u.indexOf('iPad') > -1,       // iPad
             webApp: u.indexOf('Safari') == -1,   // web应用程序
             souyue: u.indexOf('souyue') > -1,
             superapp: u.indexOf('superapp') > -1,
             weixin:u.toLowerCase().indexOf('micromessenger') > -1,
             Safari:u.indexOf('Safari') > -1
          };
      }(),
           language: navigator.language.toLowerCase()
   };
   // 点击
   $('#btnWechat').click(function () {
      var tVal = $('#linkCopy').text();
      var cpInput = document.createElement('input'); //创建一个对象
      cpInput.value = tVal;
      document.body.appendChild(cpInput); //appendChild()方法在节点的子节点末添加新的子节点
      cpInput.select();  //选择对象
      document.execCommand("Copy"); //执行浏览器复制命令
      cpInput.className = 'cpInput';
      cpInput.style.display='none';
      alert("复制成功,在微信搜索处粘贴即可");
      // ios、android
      if (browser.versions.ios) {
          window.location.href = 'weixin://';
      }else if (browser.versions.android){
          var ifr = document.createElement("iframe");
          ifr.src = 'weixin://';
          ifr.style.display = "none";
          document.body.appendChild(ifr);
      }
   });
</script>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸曦穆泽

您的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值