效果图
HTML代码
html
<div>链接:
<span style="color: #FF8A21" @click="CopyUrl" id="copyId"> https://blog.csdn.net/qq_51463650?spm=1000.2115.3001.5343</span>
</div>
复制方法
js
const CopyUrl = () => {
let url = "https://blog.csdn.net/qq_51463650?spm=1000.2115.3001.5343";
if (!url) return showToast('复制失败!')
let copyDom = document.getElementById('copyId')
copyDom.innerText = url
//创建选中范围
let range = document.createRange()
range.selectNode(copyDom)
//移除剪切板中内容
window.getSelection().removeAllRanges()
//添加新的内容到剪切板
window.getSelection().addRange(range)
//复制
let successful = document.execCommand('copy')
if (successful) {
showToast('复制成功!')
} else {
showToast('复制失败!')
}
}