<span id="copy">123456789</span>
<Button key="copyButton" onClick="toClipboard()" >
复制
</Button>
<script>
function toClipboard (){
const copySpan = document.getElementById('copy');
const range = document.createRange();
range.selectNodeContents(copySpan);
const selection = window.getSelection();
selection.removeAllRanges();//(注意)
selection.addRange(range);
const copyContent = document.execCommand('Copy',false,null);
selection.removeRange(range);
if (copyContent) {
console.log('复制成功', copyContent);
}
}
</script>
点击按钮实现复制
最新推荐文章于 2024-05-09 04:50:42 发布