当前位置: 首页 > news >正文

js实现复制拷贝的兼容方法

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。
http://www.lryc.cn/news/28297.html

相关文章:

  • 学习 Python 之 Pygame 开发魂斗罗(八)
  • Lesson11---分类问题
  • Python基础学习12——异常
  • [日常练习]练习17:链表头插法、尾插法练习
  • 第十四届蓝桥杯模拟赛(第三期)试题与题解 C++
  • 关于 “宏“
  • 1.2 CSS标签选择器,类选择器
  • 【Linux】进程等待 | 详解 wait/waitpid 的 status 参数
  • OpenAI眼中的无线调优策略
  • DataX入门
  • 第二章SpringBoot基础学习
  • B - Build Roads (最小生成树 + 打表)
  • k8s管理工具
  • Cannot start compiler The output path is not specified for module mystatic(已解决)
  • python入门应该怎么学习
  • 不懂命令, 如何将代码托管到Gitee上
  • Mysql常见面试题总结
  • python第一周作业
  • FLoyd算法的入门与应用
  • 303. 区域和检索 - 数组不可变
  • Spring Cloud融合Nacos配置加载优先级 | Spring Cloud 8
  • LeetCode 236.二叉树的最近公共祖先
  • awk简单实例(持续更新中)
  • react动态路由组件的封装
  • Vue项目中引入高德地图步骤详解
  • 软件测试用例篇(2)
  • leetcode题解-27. Remove Element
  • 【fly-iot飞凡物联】(4):在linux系统上搭建arduino环境,可以使用离线包,导入到arduino上即可。
  • java实例解析类图中【关联、组合和聚合】的区别
  • 基于m-p条件查询代码生成