当前位置:网站首页>js 实现复制功能
js 实现复制功能
2022-08-01 00:24:00 【GMLGDJ】
<body>
<button>复制</button>
<script>
const copy = (value, cb) => {
// 动态创建 textarea 标签
const textarea = document.createElement('textarea')
// 将该 textarea 设为 readonly 防止 iOS 下自动唤起键盘,同时将 textarea 移出可视区域
textarea.readOnly = 'readonly'
textarea.style.position = 'absolute'
textarea.style.left = '-9999px'
// 将要 copy 的值赋给 textarea 标签的 value 属性
// 网上有些例子是赋值给innerText,这样也会赋值成功,但是识别不了\r\n的换行符,赋值给value属性就可以
textarea.value = value
// 将 textarea 插入到 body 中
document.body.appendChild(textarea)
// 选中值并复制
textarea.select()
textarea.setSelectionRange(0, textarea.value.length)
document.execCommand('Copy')
document.body.removeChild(textarea)
if (cb && Object.prototype.toString.call(cb) === '[object Function]') {
cb()
}
}
const btn = document.getElementsByTagName('button')[0];
btn.addEventListener('click', (e) => {
console.log(e);
copy(123, () => {
console.log('复制成功');
})
})
</script>
</body>边栏推荐
猜你喜欢

cobaltstrike

/etc/sysconfig/network-scripts 配置网卡
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation

Matlab/ArcGIS processing GPM global monthly precipitation data

/etc/sysconfig/network-scripts configure the network card

微信小程序之小程序页面语法

TFC CTF 2022 WEB Diamand WriteUp

SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development

2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法

Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
随机推荐
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
Shell common script: Nexus batch upload local warehouse script
[Microservice] Distributed Transaction Solution - Seata
In 2022, the latest eight Chongqing construction members (electrical construction workers) simulation question bank and answers
开源好用的 流程图绘制工具 drawio
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
Qlib quantitative source analysis: qlib/qlib/contrib/model/GBDT py
面试题:实现死锁
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle
IPD process terminology
cobaltstrike
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
对象缓存服务的思考和实现
硬件设备计算存储及数据交互杂谈
vim的基本使用-命令模式
Keil nRF52832 download failed
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
NIO programming
The difference between /usr/local/bin and /usr/bin