当前位置:网站首页>js复制文本到剪切板
js复制文本到剪切板
2022-07-13 17:36:00 【Jason秀啊】
思路
- 创建文本域,并把需要复制的字符串赋值给value属性
- 选中(聚焦)文本域
- 执行复制操作
document.execCommand('Copy')
实现
/** * @param url 需要复制的字符串 * @param success 复制成功的回调 * @param error 复制失败的回调 */
export const copyStr = (url: string, success?: () => void, error?: () => void) => {
if (!url) return;
try {
let textArea: HTMLTextAreaElement;
textArea = document.createElement('textArea') as HTMLTextAreaElement;
textArea.value = url;
document.body.appendChild(textArea);
textArea.select();
if (document.execCommand('Copy')) {
success && success();
} else {
error && error();
}
document.body.removeChild(textArea);
} catch (err) {
error && error();
}
};
边栏推荐
猜你喜欢

Flink CDC + oceanbase full incremental integrated data integration scheme
![[hero planet July training leetcode problem solving daily] day 13 two-way linked list](/img/88/ffd37493f0adea18ee5b988e5cef9b.png)
[hero planet July training leetcode problem solving daily] day 13 two-way linked list

Call GPU completely through torch on pycharm (the most detailed)

基于 conda 的在 Win10 上从零开始搞一个 Tensorflow-gpu (2.6.0) 环境的操作记录 (2022.07)

Dynamic programming leetcode509 Fibonacci number

流批一体在京东的探索与实践

解决IDEA2020连接MySQL8出现unable to load authentication plugin ‘caching_sha2_password‘异常

SNMP起步啦

The 13th provincial competition of Bluebridge cup single chip microcomputer

快手实时数仓保障体系研发实践
随机推荐
21天SQL打卡总结
Summary of wechat red envelope project test
keil报错: ERROR 118 (ERRONEOUS REFERENCE TO EXTERNAL VARIABLES) 解决方案及原因
解决IDEA2020连接MySQL8出现unable to load authentication plugin ‘caching_sha2_password‘异常
【Tensorflow2】AttributeError: ‘Tensor‘ object has no attribute ‘numpy‘ 解决 (tf.py_function)
基于ssm项目中pagehelper分页插件的使用(简单易上手)
【黄啊码】PHP实现文件下载,支持断点续传
40.js--同名标识符提升问题
在线多行文本批量正则替换添加后缀工具
<statement> or DELIMITER expected, got ‘id‘
CONDA based operation record of starting a tensorflow GPU (2.6.0) environment on win10 from scratch (2022.07)
【黄啊码】PHP配合微信公众号生成推广二维码
Design and implementation of an eight bit compensator based on logisim
遍历器总结
对象成员检测
微信红包项目测试小结
快手实时数仓保障体系研发实践
Idea自动化生成数据库中对应表的实体类
Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
Flink CDC + oceanbase full incremental integrated data integration scheme