vue实现复制内容到粘贴板
 
方案:找到textarea对象(input同样适用),获取焦点,选中textarea的所有内容,并调用document.execCommand("copy")
 
实现代码:
 
  1. <template>
  2. <div>
  3. <textarea ref="letters"></textarea>
  4. <button @click="copyToClipboard('letters')">复制</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. loading: false
  12. }
  13. },
  14. created() {
  15. this.$nextTick(function () {
  16. this.$refs.letters.value = '用户名:张三\n性别:男\n电话号码:15812322222';
  17. })
  18. },
  19. methods: {
  20. //复制内容到粘贴板
  21. copyToClipboard(elemRef) {
  22. let target;
  23. let succeed = false;
  24. if(this.$refs[elemRef]){
  25. target = this.$refs[elemRef];
  26. // 选择内容
  27. let currentFocus = document.activeElement;
  28. target.focus();
  29. target.setSelectionRange(0, target.value.length);
  30. // 复制内容
  31. try {
  32. succeed = document.execCommand("copy");
  33. alert("内容复制成功");
  34. } catch (e) {
  35. succeed = false;
  36. }
  37. // 恢复焦点
  38. if (currentFocus && typeof currentFocus.focus === "function") {
  39. currentFocus.focus();
  40. }
  41. }
  42. return succeed;
  43. },
  44. }
  45. }
  46. </script>
 
复制完成后,在记事本等编辑器中粘贴即可。
 
 
 

vue复制textarea文本域内容到粘贴板的更多相关文章

  1. textarea文本域的高度随内容的变化而变化

    用css控制textarea文本域的高度随内容的变化而变化,不出现滚动条. CSS代码: 复制代码 代码如下: .t_area{ width:300px; overflow-y:visible } & ...

  2. div模拟textarea文本域轻松实现高度自适应——张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1362 一.关于tex ...

  3. 使用contenteditable+div模拟textarea文本域实现高度自适应

    使用contenteditable+div模拟textarea文本域实现高度自适应 开发过程中由于需要在发送消息的时候需要有一个可以高度自适应的文本域,一开始是使用textarea并搭配auto-si ...

  4. Kindeditor富文本实现textarea文本域的上传及单独button 按钮绑定(用来实现单文件上传)

    在最近项目要新增一个内容文章,文章包含一般的正文内容,其中正文中可以包含多张图片.文章最多包含一个音频文件.文章正文的上传功能我是通过textarea文本域绑定kindeditor编辑器实现的,而单独 ...

  5. js插件实现点击复制内容到粘贴板,兼容IE8

    先来看下本次需要导入的文件: 第一个是jquery.js,这个不多说: 第二个是jquery.zclip.js,第三个是zeroClipboard.swf ,这两个文件的下载链接:http://www ...

  6. js点击按钮复制内容到粘贴板

    复制内容到粘贴板,就是要选择需要复制的内容并执行document.execCommand("copy")命令: //复制内容到粘贴板 function copyToClipboar ...

  7. textarea文本域轻松实现高度自适应

    转载:http://www.xuanfengge.com/textarea-on-how-to-achieve-a-high-degree-of-adaptive.html 今天需要些一个回复评论的页 ...

  8. css之——div模拟textarea文本域的实现

    1.问题的出现: <textarea>标签为表单元素,但一般用于多行文本的输入,但是有一个明显的缺点就是不能实现高度自适应,内容过多就回出现滚动条. 为了实现高度自适应:用div标签来代模 ...

  9. div模拟textarea文本域轻松实现高度自适应

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Dart静态方法、对象操作符和类的继承

    /* Dart中的静态成员: 1.使用static 关键字来实现类级别的变量和函数 2.静态方法不能访问非静态成员,非静态方法可以访问静态成员 */ // class Person { // stat ...

  2. openresty开发系列18--lua的字符串string操作

    openresty开发系列18--lua的字符串string操作 string的相关操作 1)string.upper(s)接收一个字符串 s,返回一个把所有小写字母变成大写字母的字符串.print( ...

  3. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  4. C++ STL排序问题

    /* stl排序 */ #include <iostream> #include <map> #include <vector> #include <list ...

  5. 简单明了的注解,读取CLASS中的注解

    /***********注解声明***************/ /** * 水果名称注解 * @author peida * */ @Target(ElementType.FIELD) @Reten ...

  6. Docker:学习笔记(1)——基础概念

    Docker:学习笔记(1)——基础概念 Docker是什么 软件开发后,我们需要在测试电脑.客户电脑.服务器安装运行,用户计算机的环境各不相同,所以需要进行各自的环境配置,耗时耗力.为了解决这个问题 ...

  7. Python:实现图片裁剪的两种方式——Pillow和OpenCV

    原文:https://blog.csdn.net/hfutdog/article/details/82351549 在这篇文章里我们聊一下Python实现图片裁剪的两种方式,一种利用了Pillow,还 ...

  8. 客户端连接Codis集群

    新建maven webapp项目 添加相关依赖: <dependency> <groupId>redis.clients</groupId> <artifac ...

  9. SCI-hub使用技巧(下载外文文献)

    下载外文文献方法指南: (1)首先查找需要下载文献的DOI (2)在Sci-Hub主页搜索框输入URL.DOI或者PMID. (3)点击open即可看见下载界面. 参考文献:https://mp.we ...

  10. [转帖]HAProxy 7层 负载均衡

    HAProxy 7层 负载均衡 https://www.cnblogs.com/jicki/p/5546902.html HAProxy 系统 CentOS 5.8 x64 wget http://h ...