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>

 

posted @ 2021-05-05 15:46  陳丶  阅读(93)  评论(0编辑  收藏  举报