js+html5点击赋值到剪贴板

!DOCTYPE htmlhtml lang=enheadmeta charset=UTF-8titleTitle/title/headbodyspan id=a_1复制成功/spanbutton onclick=copyUrl(1)复制/buttonscriptfunction copyUrl(id) {const...

<!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>

 

本文标题为:js+html5点击赋值到剪贴板

基础教程推荐