點選按鈕複製文字
阿新 • • 發佈:2018-11-13
點選按鈕,進行文字複製操作。實現這個功能需要二點;
一:用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>