点击按钮,进行文本复制操作。实现这个功能需要二点;
一:用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>