原生js实现复制功能

DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>testtitle>
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>

 

你可能感兴趣的:(原生js实现复制功能)