原生js实现复制功能

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>test</title>
</head>
<body>
  <div>
    <span id="copyMy" style="position: absolute;left: -100000px;">这是复制的内容</span>
    <button onClick="copyFn()">点击复制</button>
  </div>
  <script>
    function copyFn() {
      var val = document.getElementById('copyMy');
      window.getSelection().selectAllChildren(val);
      document.execCommand("Copy");
    }
  </script>
</body>
</html>

 

posted @ 2020-06-16 14:27  青云码上  阅读(375)  评论(0编辑  收藏  举报