当前位置:网站首页>JS实现复制内容到剪贴板的方法
JS实现复制内容到剪贴板的方法
2022-06-09 14:30:00 【别盯着我的名字看】
复制方法
/**
* 复制功能
* text 传入要复制的文本
* callback 复制完成后的回调
*/
function copy(text, callback) {
// 实现复制方法一:
if (navigator.clipboard) {
navigator.clipboard.writeText(text);
callback && callback(true);
return;
}
// 实现复制方法二:
var copyInput = document.getElementById('copyInput');
if (!copyInput) {
var copyInput = document.createElement('input');
copyInput.setAttribute('id', 'copyInput');
copyInput.style.position = 'fixed';
copyInput.style.left = '-100%';
copyInput.style.top = '0';
copyInput.style.zIndex = -100;
copyInput.style.opacity = 0;
document.body.appendChild(copyInput);
}
copyInput.value = text;
copyInput.focus();
copyInput.select();
// document.execCommand 可能会被废弃
if (document.execCommand('copy')) {
document.execCommand('copy');
}
copyInput.blur();
callback && callback(true);
}调用方法
copy('传入你要复制的文本');相关文章
边栏推荐
猜你喜欢

陕西西安等保测评单位有哪些?在哪里可以查到?

数据库中错误1062,报错

为什么 SQL 语句使用了索引,但却还是慢查询?

今天19:30 | 图形学专场—中国科学院计算技术研究所高林老师团队

Web3 的“中国特色”

鸿蒙移植i.mx6ull(六) Kconfig_GCC_Mkefile

ECCV 2020 | STAR:基于Transformer的行人轨迹预测模型(二)

Su Tao: application of counter sample technology in the field of Internet Security

【实战】Redis Cluster(下)-系统版本支持问题

C listbox usage
随机推荐
信息学奥赛一本通 1260 【例9.4】拦截导弹(Noip1999) | 洛谷 P1020 [NOIP1999 普及组] 导弹拦截
道友,你都用Redis做啥了?
华为哈勃将再添IPO,美芯晟蛰伏十余年后冲刺科创板
497. 非重叠矩形中的随机点
详解工厂方法
鸿蒙移植i.mx6ull(六) Kconfig_GCC_Mkefile
【论文】Cascade RPN: Delving into high-quality region proposal network with Adaptive Convolution
FCPX插件:动态物体运动模糊视觉特效Motion Blur FX by MA
自如涨价,会逼退年轻人吗?
薪酬不变,每周只上四天班,英国试行全球最大规模“四天工作制”
From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from
鸿蒙移植i.mx6ull(五) 移植概述
请教股票怎么在手机上开户流程?股票开户流程网上开户安全吗?
@Enablefeignclients annotation source code analysis
CICC digital intelligence China II: five questions and five answers to database business market
Taoist friend, what have you done with redis?
文化和自然遗产日,任务空投来了
期货开户去哪里开?网上开户安不安全?
【论文】Cascade R-CNN: Delving into High Quality Object Detection
Avoid abusing class styles