Js实现一键复制小功能
2024-10-21 05:33:47
function copyToClipboard(textToCopy) {
// navigator clipboard 需要https等安全上下文
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard 向剪贴板写文本
return navigator.clipboard.writeText(textToCopy).then(function () {
layer.msg("复制成功Https!", { icon: 1 })
});
} else {
// 创建text area
let textArea = document.createElement("textarea");
textArea.value = textToCopy;
// 使text area不在viewport,同时设置不可见
textArea.style.position = "absolute";
textArea.style.opacity = 0;
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
// 执行复制命令并移除文本框
layer.msg("复制成功Http!", { icon: 1 });
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
}
最新文章
- shell script 执行常用的两种方式
- 使用keychain保存用户名和密码等敏感信息 KeychainItemWrapper和SFHFKeychainUtils
- 关于Android中查看app安装时间等信息的问题
- FileZilla FTP Server 高级防火墙例外
- IE6-BUG-CSS兼容详解
- iOS_SN_LLDB常用命令
- solr 分词词库管理思路
- 用VS Code体验调试.NET Core 2.0 Preview (传统三层架构)
- yaf代码生成工具的使用
- CentOS6.8 安装node.js npm
- 2.静态AOP实现-装饰器模式
- Zend Optimizer,Zend Guard Loader 和 Zend Opcache 三者之间的区别
- TensorFlow占位符操作:tf.placeholder_with_default
- oracle 行号和分页
- POJ 3579 3685(二分-查找第k大的值)
- 启动exe
- Dubbo集群配置和官方文档
- win7开始菜单路径
- 微信小程序奇奇怪怪的语法
- 如何释放vector变量