點擊按鈕,進行文本復制操作。實現這個功能需要二點;
一:用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>
