码迷,mamicode.com
首页 > 其他好文 > 详细

点击按钮复制文本

时间:2018-11-13 21:45:33      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:成功   获取   pyw   function   gets   alert   span   hat   col   

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

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

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

关键代码

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

 

HTML

<p id="copyWeChat">要复制的内容</p>

<span onclick="copyFn(‘copyWeChat‘)">复制</span>

 

JS

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

 

点击按钮复制文本

标签:成功   获取   pyw   function   gets   alert   span   hat   col   

原文地址:https://www.cnblogs.com/zimengxiyu/p/9954856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!