html:
<div id="box">复制</div>
<div id="copy">123456789</div>
js:
function copyFn(event) {
const range = document.createRange();
range.selectNode(document.getElementById('copy'));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
console.log("复制成功")
}
document.getElementById('box').addEventListener('click', copyFn, false);
网友评论