原生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>
原文地址:https://www.cnblogs.com/djjlovedjj/p/13140757.html