js原生实现复制内容到粘贴板
作者:
gem_Y | 来源:发表于
2020-01-19 13:37 被阅读0次 private copyUrl = (url: string) => () => {
let textArea = document.createElement('textArea')
textArea.innerText = url
document.body.appendChild(textArea)
// console.log(textArea)
var range = document.createRange(); //创建一个range
window.getSelection().removeAllRanges(); //清除页面中已有的selection
range.selectNode(textArea); // 选中需要复制的节点
window.getSelection().addRange(range); // 执行选中元素
let successful = document.execCommand('copy'); // 执行 copy 操作
if(successful){
message.success('复制成功')
}else{
message.error('复制失败,请手动复制')
}
// 移除选中的元素
window.getSelection().removeAllRanges();
// 移除 创建的元素
document.body.removeChild(textArea)
}
本文标题:js原生实现复制内容到粘贴板
本文链接:https://www.haomeiwen.com/subject/gserzctx.html
网友评论