使用JS实现复制粘贴功能
2024-09-01 04:58:52
使用JS实现复制粘贴功能
如果嵌套太多使用这个:
// 複製單號1
// 第一步把這個放到頁面
// <div style="position:absolute; opacity: 0;" id="copy1"></div>
// 第二步增加按鈕設置點擊事件
// 第三如下:
copyNo() {
const a = document.createElement('textarea')
let b;
if (this.myshow) {
a.innerText = this.aaa[this.aaa.ccc] || '';
} else {
a.innerText = this.bb|| '';
}
b = document.getElementById('copy1')
a.setAttribute('id', 'copy');
b.innerHTML = "";
b.appendChild(a)
let e: any = document.getElementById("copy");
e.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
if (a.value) {
alert('Copy Success!');
} else {
alert('The copied content is empty!');
}
}
// 複製單號2
如果是简单地输入框没有太多嵌套,使用这个:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function copyLink(){
var e = document.getElementById("copy");
e.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
alert("内容复制成功!");
}
</script>
</head>
<body>
<!-- <textarea id="copy">待复制的内容</textarea> -->
<input type="text" id="copy">
<input type="button" onclick="copyLink()" value="点击复制"></input>
</body>
</html>
最新文章
- Codeforces Round #389 (Div. 2,) B C
- 解决 笔记本键盘打字母却跳出数字来,每次都要按一遍Fn+Num LK 的问题
- Intel VT-x 基本概念
- How to generate ssh key only for github and not conflict with original key
- InAction-编译hadoop2.5.2
- php多线程即时通讯
- android Canvas drawText 文字居中
- Okhttp3日志采集功能
- freemarker报错之三
- HTTP协议3之压缩--转
- ArrayList与LinkedList时间复杂度之对比
- ci框架中输出sql语句
- 图文详解linux/windows mysql忘记root密码解决方案
- 我把一些Linux的中英文命令做了对应翻译大家参考一下
- 暑期培训7日游解题思路(day1~day3)
- ArcGIS AddIn调用ArcMap自带的对话框
- springmvc实现文件上传
- java swing示例
- css进阶篇
- jquery中 苹果手机对on触发的点击事件无效果