H5移动端复制功能实现

摘要:
1//单击复制信息2copyTxt(txt){3varu=navigator.userAgent4varisAndroid=u.indexOf('Android')˃-1||u.indexOf('Linux')˃-15//您应该首先确定当前系统,否则将报告错误。无法执行语句6if(isAndroid){7let_input=document.createElement('input')。//直接构建
 1     // 点击复制信息
 2     copyTxt (txt) {
 3       var u = navigator.userAgent
 4       var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1
 5       // 要先判断当前是什么系统,否则会报错,无法执行语句
 6       if (isAndroid) {
 7         let _input = document.createElement('input')// 直接构建input
 8         _input.value = txt// 设置内容
 9         document.body.appendChild(_input)// 添加临时实例
10         _input.select()// 选择实例内容
11         document.execCommand('Copy')// 执行复制
12         document.body.removeChild(_input)// 删除临时实例
13         if (document.execCommand('Copy')) {
14           Toast('复制成功')
15         } else {
16           Toast('复制失败,请手动复制')
17         }
18       } else {
19         // 数字没有 .length 不能执行selectText 需要转化成字符串
20         const textString = txt.toString()
21         let input = document.querySelector('#copy-input')
22         if (!input) {
23           input = document.createElement('input')
24           input. 
25           input.readOnly = 'readOnly'
26           input.style.position = 'absolute'
27           input.style.left = '-1000px'
28           input.style.zIndex = '-1000'
29           document.body.appendChild(input)
30         }
31 
32         input.value = textString
33         // ios必须先选中文字且不支持 input.select()
34         this.selectText(input, 0, textString.length)
35         console.log(document.execCommand('copy'), 'execCommand')
36         if (document.execCommand('copy')) {
37           document.execCommand('copy')
38           Toast('复制成功')
39         } else {
40           Toast('复制失败,请手动输入')
41         }
42         input.blur()
43         document.body.removeChild(input)
44         // input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法
45         // 选择文本。createTextRange(setSelectionRange)是input方法
46       }
47     },
48     selectText (textbox, startIndex, stopIndex) {
49       if (textbox.createTextRange) { // ie
50         const range = textbox.createTextRange()
51         range.collapse(true)
52         range.moveStart('character', startIndex)// 起始光标
53         range.moveEnd('character', stopIndex - startIndex)// 结束光标
54         range.select() // 不兼容苹果
55       } else { // firefox/chrome
56         textbox.setSelectionRange(startIndex, stopIndex)
57         textbox.focus()
58       }
59     }

免责声明:文章转载自《H5移动端复制功能实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇12月1号 MacOS系统命令行操作方式MacOs终端忽略大小写下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【Android】是时候为你的应用加上WebDav同步了

WebDav是什么? WebDAV (Web-based Distributed Authoring and Versioning) 一种基于HTTP1.1协议的通信协议。它扩展了HTTP 1.1,在GET、POST、HEAD等几个HTTP标准方法以外添加了一些新的方法,使应用程序可对Web Server直接读写,并支持写文件锁定(Locking)及解锁(...

002输入子系统驱动

输入子系统概念介绍(第十三课/第一节) 回顾第三个驱动程序(中断方式的按键驱动程序)和测试程序,发现有一些缺点:这个驱动程序没办法用在别人写的现成的应用程序上(比如:QT),因为别人写的应用程序肯定不会来打开你这个"/dev/third_chrdev"。别人打开的是一些现成的设备(比如:/dev/tty),甚至别人都不打开设备,而是直接调用 scanf()...

用户输入和while循环

函数input()的工作原理 message=input('Tell me something,and I will repeat it back to you:') print(message) 编写清晰的程序 #有时,提示可能超过一行,可将提示存储在一个变量中,再将该变量传递给函数input()。 prompt='If you tell us who...

easy ui 给表单元素赋值input,combobox,numberbox

①给input控件 class="easyui-textbox" <input data-options="required:true" type="text" name="RelCompanyName" />$("#RelCompanyName").attr('resize','140px');//设置宽度$("#RelCompanyName...

Android随笔之——用shell脚本模拟用户按键、触摸操作

  之前写过两篇关于Android中模拟用户操作的博客(其实用一篇是转载的),现在就来讲讲用shell脚本来模拟用户按键操作。本次的目标是用shell脚本打开微信并在其搜索框中搜索相关内容。   本文的模拟功能主要是用adb的input命令来实现,如果你adb的环境变量配置正确的话,在cmd中输入 adb shell input 就可以看见input的用法...

day049--jQuery文档操作示例

DOM操作(CRUD增改查删)   创建元素 $('span') // 创建一个span标签   后置插入操作   append(), appendTo() <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <...