在移动端实现在浏览器网页点击按钮并复制文字(且测试打开微信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>
发布了52 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_41408081/article/details/105251808