实现原理

采用document.execCommand('copy')来实现复制到粘贴板功能

复制必须是选中input框的文字内容,然后执行document.execCommand('copy')命令实现复制功能。

初步实现方案

const input = document.querySelector('#copy-input');

if (input) {

input.value = text;

if (document.execCommand('copy')) {

input.select();

document.execCommand('copy');

input.blur();

alert('已复制到粘贴板');

}

}

兼容性问题

input 输入框不能hidden或者display: none;

如果需要隐藏输入框可以使用定位脱离文档流,然后移除屏幕

#copy-input{

position: absolute;

left: -1000px;

z-index: -1000;

}

2.ios下不能执行document.execCommand(‘copy’)

在ios设备下alert(document.execCommand('copy'))一直返回false

查阅相关资料发现ios下input不支持input.select();

因此拷贝的文字必须存在,不能为空字符串,不然也不会执行复制空字符串的功能

参考这篇博客实现了ios下复制的功能 https://blog.csdn.net/VLilyV/…

主要是使用textbox.createTextRange方法选中输入框的文字

// input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法

// 选择文本。createTextRange(setSelectionRange)是input方法

function selectText(textbox, startIndex, stopIndex) {

if (textbox.createTextRange) {//ie

const range = textbox.createTextRange();

range.collapse(true);

range.moveStart('character', startIndex);//起始光标

range.moveEnd('character', stopIndex - startIndex);//结束光标

range.select();//不兼容苹果

} else {//firefox/chrome

textbox.setSelectionRange(startIndex, stopIndex);

textbox.focus();

}

}

3.ios设备上复制会触发键盘弹出事件

给input加上readOnly只读属性

代码

踩完以上的坑,总结的代码如下

git地址 https://github.com/zhaosheng8…

copyText = (text) => {

// 数字没有 .length 不能执行selectText 需要转化成字符串

const textString = text.toString();

let input = document.querySelector('#copy-input');

if (!input) {

input = document.createElement('input');

input.id = "copy-input";

input.readOnly = "readOnly"; // 防止ios聚焦触发键盘事件

input.style.position = "absolute";

input.style.left = "-1000px";

input.style.zIndex = "-1000";

document.body.appendChild(input)

}

input.value = textString;

// ios必须先选中文字且不支持 input.select();

selectText(input, 0, textString.length);

console.log(document.execCommand('copy'), 'execCommand');

if (document.execCommand('copy')) {

document.execCommand('copy');

alert('已复制到粘贴板');

}

input.blur();

// input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法

// 选择文本。createTextRange(setSelectionRange)是input方法

function selectText(textbox, startIndex, stopIndex) {

if (textbox.createTextRange) {//ie

const range = textbox.createTextRange();

range.collapse(true);

range.moveStart('character', startIndex);//起始光标

range.moveEnd('character', stopIndex - startIndex);//结束光标

range.select();//不兼容苹果

} else {//firefox/chrome

textbox.setSelectionRange(startIndex, stopIndex);

textbox.focus();

}

}

};

// 复制文字

// 必须手动触发 点击事件或者其他事件,不能直接使用js调用!!!

copyText('h5实现一键复制到粘贴板 兼容ios')

/*兼容性补充:

移动端:

安卓手机:微信(chrome)和几个手机浏览器都可以用。

苹果手机:微信里面和sarafi浏览器里也都可以,

PC:sarafi版本必须在10.2以上,其他浏览器可以.

兼容性测试网站:https://www.caniuse.com/

*/

.

html 一键复制 ios,h5实现一键复制到粘贴板-兼容ios相关推荐

  1. h5实现一键复制到粘贴板 兼容iOS

    copyText = (text) => {// 数字没有 .length 不能执行selectText 需要转化成字符串const textString = text.toString();l ...

  2. h5 实现一键复制到粘贴板 兼容iOS

    效果展示 先贴上测试连接 http://cdn.foundao.com/zhaosheng/copytext 实现原理 采用 document.execCommand('copy') 来实现复制到粘贴 ...

  3. ios下js复制到粘贴板_h5实现一键复制到粘贴板 兼容ios

    实现原理 采用document.execCommand('copy') 来实现复制到粘贴板功能 复制必须是选中input框的文字内容,然后执行document.execCommand('copy') ...

  4. 通过纯js代码实现将指定内容复制到粘贴板(兼容各主流浏览器)

    通过纯js代码实现将指定内容复制到粘贴板(兼容各主流浏览器) 1.业务需求: vue项目需要通过点击button按钮将指定内容复制到粘贴板中,且不引入第三方组件. 2.解决方案: js写法如下(这里是 ...

  5. 【js】vue项目中实现点击复制过滤条件,获取并处理粘贴板内容

    前情提要 有这样一个需求:每次重复选过滤条件太麻烦啦,需要一个可以复制过滤条件的功能!过滤条件类似下图. 主要步骤 第一步:复制工具的选取.这里我选用的是原生的Document.execCommand ...

  6. 用js实现复制内容到操作系统粘贴板(兼容IE、谷歌、火狐等浏览器)

    一.如果只考虑IE浏览器,可以直接用原声js实现 if(window.clipboardData){//清空操作系统粘贴板window.clipboardData.clearData();//将需要复 ...

  7. ios java aes_PHP7 AES加密解密函数_兼容ios/andriod/java对等加解密

    **PHP7.0 7.1 7.2 7.3 AES对等加解密类 函数文件_兼容ios/andriod/java等** 由于新项目规划要求使用PHP7.2开发环境,但在部分新系统中仍需使用AES加解密方式 ...

  8. iOS开发那些事--创建基于故事板的iOS 6的HelloWorld

    基于故事板的HelloWorld工程 Storyboard(故事板)是用来替代xib的技术,也是iOS 5最重要的新特性之一.我们用Storyboard(故事板)重构HelloWorld. 使用故事板 ...

  9. H5一键复制 兼容iOS

    浏览器原生剪贴板 navigator.clipboard 1. 写入 navigator.clipboard.writeText navigator.clipboard.writeText('需要复制 ...

最新文章

  1. 谷歌自动驾驶是个大坑,还好中国在构建自己的智能驾驶大系统
  2. LOJ#510. 「LibreOJ NOI Round #1」北校门外的回忆(线段树)
  3. PING的原理以及ICMP协议
  4. Spring Boot系列——7步集成RabbitMQ
  5. ​TA们划重点的时候到了:什么是实例工作流?
  6. python实现画图哆啦A梦
  7. linux-目录命令-mk dir- cd- pwd- rm dir- cp- mv- rm
  8. C#/.Net开发入门篇(1)——开发工具安装
  9. 有哪些在朋友圈发会被秒赞的文案?
  10. 并发编程应用场景_linux网络编程之select函数的并发限制和poll函数应用举例
  11. 编译原理---代码优化基础(自己看)
  12. 使用rsyslog+loganalzey收集日志显示客户端ip
  13. 120个常用货源网站,赶紧收藏!
  14. 人物-李彦宏:李彦宏
  15. 安装破解VS2015(破解使用秘钥)
  16. ts格式转换器android,ts格式转换器下载|ts格式转换器 v6.2-520下载站
  17. C语言-Switch 语句
  18. 二进制与base64
  19. Mac搭建Linux虚拟机
  20. 数据文件online和offline

热门文章

  1. CC254x到CC2640
  2. LeetCode刷题框架总结
  3. Oracle空闲超时时间设置
  4. oracle计算两行差值
  5. Tushare数据获取(python)
  6. [4G5G专题-71]:物理层 - 4G LTE 物理混合自动重传指示信道PHICH与物理上行控制信道PUCCH与UCI
  7. 计算对数似然函数改变量
  8. java 空格 char_java中如何判断char是否是空格
  9. Java 海康SDK透明通道建立(485透传)
  10. OpenCV中threshold自动阈值,类似matlab中的graythresh