ios复制文字到剪切板

补充上篇博客 中出现的ios复制不可用的问题需要组合使用

 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <span id="data">复制我到剪贴板</span>
  <button class="button" type="button" id="button">点击复制</button>


  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>
  <script>
    $("#button").click(function () {
      if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { //ios  
        var copyDOM = document.querySelector('#data'); //要复制文字的节点    
        var range = document.createRange();
        // 选中需要复制的节点    
        range.selectNode(copyDOM);
        // 执行选中元素    
        window.getSelection().addRange(range);
        // 执行 copy 操作    
        var successful = document.execCommand('copy');
        try {
          var msg = successful ? 'successful' : 'unsuccessful';

          console.log('copy is' + msg);
        } catch (err) {
          console.log('Oops, unable to copy');
        }
        // 移除选中的元素    
        window.getSelection().removeAllRanges();
      } else {

        console.log( "android不支持此方式复制")
      }
    });

  </script>


</body>

</html>

 

posted @ 2018-04-09 11:48  monkye  阅读(214)  评论(0编辑  收藏  举报