点击按钮,进行文本复制操作。实现这个功能需要二点;

 一:用window.getSelection().selectAllChildren(“”)获取要复制的内容 

 二:用document.execCommand ("Copy");进行复制操作

关键代码

 window.getSelection().selectAllChildren(val);
 document.execCommand ("Copy");

 

HTML

<p >复制</span>

 

JS

<script>
            
            function copyFn(id){
            var val = document.getElementById(id);
            window.getSelection().selectAllChildren(val);
            document.execCommand ("Copy");
            alert("复制成功")
            }
        </script>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
相关资源
相似解决方案