使用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>

最新文章

  1. Codeforces Round #389 (Div. 2,) B C
  2. 解决 笔记本键盘打字母却跳出数字来,每次都要按一遍Fn+Num LK 的问题
  3. Intel VT-x 基本概念
  4. How to generate ssh key only for github and not conflict with original key
  5. InAction-编译hadoop2.5.2
  6. php多线程即时通讯
  7. android Canvas drawText 文字居中
  8. Okhttp3日志采集功能
  9. freemarker报错之三
  10. HTTP协议3之压缩--转
  11. ArrayList与LinkedList时间复杂度之对比
  12. ci框架中输出sql语句
  13. 图文详解linux/windows mysql忘记root密码解决方案
  14. 我把一些Linux的中英文命令做了对应翻译大家参考一下
  15. 暑期培训7日游解题思路(day1~day3)
  16. ArcGIS AddIn调用ArcMap自带的对话框
  17. springmvc实现文件上传
  18. java swing示例
  19. css进阶篇
  20. jquery中 苹果手机对on触发的点击事件无效果

热门文章

  1. 2.签名&amp;初始化&amp;提交
  2. 27.-Django发送邮件
  3. 【单元测试】Junit 4(三)--Junit4断言
  4. Go实现栈与队列基本操作
  5. MySQL数据库的性能分析 ---图书《软件性能测试分析与调优实践之路》-手稿节选
  6. Go语言输出函数fmt.Print、fmt.Printf、fmt.Println的用法区别
  7. C++编程笔记(QT)
  8. 使用Typora
  9. Anaconda下载安装
  10. Jmeter 之 switch 控制器