js实现复制拷贝的兼容方法
创始人
2024-05-28 13:02:56
0

1. 定义复制拷贝的方法

在某个工具类方法中定义该方法,兼容不同浏览器处理

/*** @description 拷贝的类方法*/
class CopyClass {// constructor() {}setRange(input) {return new Promise((resolve, reject) => {try {//  创建range对象const range = document.createRange();// 获取复制内容的 id 选择器range.selectNode(input);// 创建 selection对象const selection = window.getSelection();// 如果页面已经有选取了的话,会自动删除这个选区,没有选区的话,会把这个选取加入选区if (selection.rangeCount > 0) {selection.removeAllRanges();} else {//}// 将range对象添加到selection选区当中,会高亮文本块selection.addRange(range);resolve();} catch (error) {reject(error);}});}createInput(val) {return new Promise((resolve, reject) => {try {const input = document.createElement('input'); // 创建input对象input.type = 'hidden';input.value = val;resolve(input);} catch (error) {reject(error);}});}execCommand() {return new Promise((resolve, reject) => {try {//  该方法已弃用document.execCommand('Copy');resolve();} catch (error) {reject(error);}});}inputSelectCopy(input) {return new Promise((resolve, reject) => {try {document.body.appendChild(input);input.select(); // 选择对象;this.execCommand().then(() => {input.remove();resolve();}).catch(e => {reject(e);});} catch (error) {reject(error);}});}clipboardWriteText(val) {return new Promise((resolve, reject) => {try {navigator.clipboard.writeText(val).then(res => {resolve();}).catch(e => {reject(e);});} catch (error) {reject(error);}});}copy(val) {return new Promise((resolve, reject) => {if (val && typeof val === 'string') {const errorObj = new Error('页面复制内容失败!');this.clipboardWriteText(val).then(res => {resolve();}).catch(e => {this.createInput(val).then(res => {this.setRange(res).then(() => {this.execCommand().then(() => {resolve();}).catch(e => {reject(errorObj);});}).catch(e => {this.inputSelectCopy(res).then(() => {resolve();}).catch((e) => {reject(errorObj);});});}).catch(e => {reject(errorObj);});});} else {reject(new Error('不能复制无效内容!'));}});}
}/*** @description 拷贝*/
export function copy(val) {const copyClass = new CopyClass();return copyClass.copy(val);
}

2. 实际调用

import copy from '@/utils/index';copy('需要拷贝的内容').then((res) => {//	成功提示
}).catch(e => {//	报错提示
});

3. 涉及方法

  1. document.createRange: 返回一个 Range 对象。
  2. selectNode: Range.selectNode() 方法将 Range 设置为包含整个 Node 及其内容。Range 的起始和结束节点的父节点与 referenceNode 的父节点相同。
  3. window.getSelection: 返回一个 Selection 对象,表示用户选择的文本范围或光标的当前位置。
  4. removeAllRanges: 从Selection对象里移除Range对象。
  5. addRange: 将一个Range对象添加到Selection对象中。
  6. document.execCommand: 当一个 HTML 文档切换到设计模式时,document暴露 execCommand 方法,该方法允许运行命令来操纵可编辑内容区域的元素。这里我们使用copy命令来复制所选内容。 需要注意的是,该方法已经被弃用了!!!
  7. navigator.clipboard.writeText: Clipboard 接口的 writeText() 方法可以写入特定字符串到操作系统的剪切板, 该方法返回一个关于执行情况的Promise。

相关内容

热门资讯

英语作文书信(优质6篇) 英语作文书信 篇一Dear Peter,I hope this letter finds you i...
介绍自己英语作文 介绍自己英语作文(通用34篇)  在日常学习、工作或生活中,说到作文,大家肯定都不陌生吧,根据写作命...
英语作文给家长写信范文【最新... 英语作文给家长写信范文 篇一Dear Mom and Dad,I hope this letter ...
阿甘正传观后感英文版(优秀3... 阿甘正传观后感英文版 篇一Title: The Inspirational Journey of F...
植树节英语作文【精选6篇】 植树节英语作文 篇一Planting Trees on Arbor DayArbor Day is ...
我的生活英语作文(通用6篇) 我的生活英语作文 篇一My Daily RoutineMy daily routine starts...
英语词语辨析:because... 英语词语辨析:because与 because of的区别 篇一因果关系的表达是英语中非常重要的语法...
英语范文我的旅行计划18篇(... 英语范文我的旅行计划18篇 篇一My Travel Plan to ParisParis, the ...
初见英语老师200字作文【优... 初见英语老师200字作文 篇一初见英语老师初中时的我,对英语可谓是一无所知。记得那年开学第一天,我们...
快乐的一天英语作文【精简3篇... 快乐的一天英语作文 篇一:我的快乐一天Today was a truly happy day for...
英语词语学习:outsour... 英语词语学习:outsource 篇一Outsource: Understanding the Co...
创造性英语征文范文【优秀6篇... 创造性英语征文范文 篇一标题:The Power of ImaginationImagination...
青年英语作文(通用6篇) 青年英语作文 篇一:如何提高英语口语能力In today's globalized world, h...
top of the wor... top of the world歌词  top of the world是一首极为经典的英文歌曲,中...
英语四级写作常用单词 英语四级写作常用单词  在学习、工作乃至生活中,大家一定都接触过英语吧,下面是小编收集整理的英语四级...
事业目标英语范文初一(优选6... 事业目标英语范文初一 篇一My Career GoalsAs a freshman in high ...
网购的英语作文【精彩3篇】 网购的英语作文 篇一Online ShoppingIn recent years, online s...
童话故事英语作文【优秀3篇】 童话故事英语作文 篇一: The Magic AppleOnce upon a time, in a...
英语作文范文关于理想工作(通... 英语作文范文关于理想工作 篇一Title: My Ideal JobIntroduction:Eve...
英语朗读比赛作文【实用5篇】 英语朗读比赛作文 篇一The Power of ReadingReading is a powerf...