js+html5点击赋值到剪贴板

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

  <head>
    <meta charset="UTF-8">
    <title>Title</title>
  </head>
  <body>
    <span id="a_1">复制成功</span>
    <button onclick="copyUrl(1)">复制</button>
    <script>
        function copyUrl(id) {const range = document.createRange();//创建range对象
            range.selectNode(document.getElementById('a_'+id));
            const selection = window.getSelection();//选择的文本范围
            selection.addRange(range);//向选区(Selection)中添加一个区域(Range)
            document.execCommand('Copy');//将当前选中区复制到剪贴板
            console.log("复制成功!");
      }

    </script>
  </body>

</html>

 

上一篇:基础算法之简单选择排序(selection sort)


下一篇:Unit 11