复制文字或图片
2022-03-07 本文已影响0人
红酒煮咖啡
import { message } from 'antd'
export const copyText = (type, content) => {
//type==''div"||"img" 文字传div
const div = document.createElement(type)
type == 'img' ? (div.src = content) : (div.innerHTML = content)
document.getElementById('app').appendChild(div)
window.getSelection().removeAllRanges()
const range = document.createRange()
window.getSelection().addRange(range)
range.selectNode(div)
document.execCommand('copy')
message.success(type == 'img' ? '图片复制成功' : '文本复制成功')
div.remove()
window.getSelection().removeAllRanges()
}