如题
由于electron项目需要自定义右键事件,需要上线文本复制粘贴,图片的复制粘贴等等功能
其他都是简单,在图片选择复制遇上了困难,不多说,直接上添加方法
private handleItemClick(type:string,e?:MouseEvent){
//ts 方式的获取元素节点 <img ref='img'src='....'></img>
const imgName = 'img';
const img: any = this.$refs[imgName];
let selection = window.getSelection();
let range = document.createRange();
if(selection){
range.selectNode(img as Element);
selection.removeAllRanges();
selection.addRange(range);
(img as Element).ownerDocument.execCommand('copy');
selection.removeAllRanges();
}
}
就这样~,不知道range.selectNode苦恼了我非常多的时间。