前端页面中很多时候我们需要来个快捷的【复制】按钮,不需要拖着鼠标选择那么多文本了,直接点击按钮就行了。
- 实现方式:
// 复制文本
export function copyText(text) { // text: 要复制的内容
// 方法一:
// const tag = document.createElement('input')
// tag.setAttribute('id', 'copy_input_id')
// tag.value = text
// document.getElementsByTagName('body')[0].appendChild(tag)
// const obj = document.getElementById('copy_input_id')
// obj && obj.select()
// document.execCommand('copy')
// obj && obj.remove()
// message.destroy()
// message.success('复制成功')
// 方法二:
navigator.clipboard.writeText(text);
message.success('复制成功')
}