Skip to content

2018-05-28最新兼容ios和andorid的复制粘贴方法

代码如下

html
<!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>
    <style>
      input {
        position: absolute;
        top: 0;
        left: -200px;
        color: transparent;
        background: transparent;
      }
    </style>
  </head>
  <body>
    <input type="text" readonly="true" value="你阿达来垃圾袋爱上邓丽君" />
    <button>复制到剪切板</button>
  </body>
  <script>
    var oPara = document.getElementsByTagName("input")[0];
    var oButton = document.getElementsByTagName("button")[0];
    oButton.onclick = function () {
      //  oPara.select();//ios不支持这种方法,但并不报错
      oPara.setSelectionRange(0, oPara.value.length); //安卓和ios都支持
      document.execCommand("copy");
    };
  </script>
</html>

以上

上次更新于: