2023年6月21日发(作者:)
uni-app兼容H5和⼩程序的setClipboardDatauni-app兼容H5和⼩程序的setClipboardData,⽬前只测试微信⼩程序和H5。const setClipboardData = (option)=>{ !document && pboardData(option) document && copyText(option)}const copyText = (option) => { // 数字没有 .length 不能执⾏selectText 需要转化成字符串 const textString = ng(); let input = elector('#copy-input'); if (!input) { input = Element('input'); = "copy-input"; ly = "readOnly"; // 防⽌ios聚焦触发键盘事件 on = "fixed"; = "-1000px"; = "-1000"; Child(input) } = textString; // ios必须先选中⽂字且不⽀持 (); selectText(input, 0, ); if (mmand('copy')) { mmand('copy'); s && s('success') te && te('success') }else { && ('fail') te && te('fail') } (); };// input⾃带的select()⽅法在苹果端⽆法进⾏选择,所以需要⾃⼰去写⼀个类似的⽅法 // 选择⽂本。createTextRange(setSelectionRange)是input⽅法 const selectText = (textbox, startIndex, stopIndex)=> { if (TextRange) {//ie const range = TextRange(); se(true); art('character', startIndex);//起始光标 d('character', stopIndex - startIndex);//结束光标 ();//不兼容苹果 } else {//firefox/chrome ectionRange(startIndex, stopIndex); (); } } // 复制⽂字 // 必须⼿动触发 点击事件或者其他事件,不能直接使⽤js调⽤// copyText('h5实现⼀键复制到粘贴板 兼容ios') /*兼容性补充: 移动端: 安卓⼿机:微信(chrome)和⼏个⼿机浏览器都可以⽤。 苹果⼿机:微信⾥⾯和sarafi浏览器⾥也都可以, PC:sarafi版本必须在10.2以上,其他浏览器可以. 兼容性测试⽹站:/*/export default { setClipboardData}
2023年6月21日发(作者:)
uni-app兼容H5和⼩程序的setClipboardDatauni-app兼容H5和⼩程序的setClipboardData,⽬前只测试微信⼩程序和H5。const setClipboardData = (option)=>{ !document && pboardData(option) document && copyText(option)}const copyText = (option) => { // 数字没有 .length 不能执⾏selectText 需要转化成字符串 const textString = ng(); let input = elector('#copy-input'); if (!input) { input = Element('input'); = "copy-input"; ly = "readOnly"; // 防⽌ios聚焦触发键盘事件 on = "fixed"; = "-1000px"; = "-1000"; Child(input) } = textString; // ios必须先选中⽂字且不⽀持 (); selectText(input, 0, ); if (mmand('copy')) { mmand('copy'); s && s('success') te && te('success') }else { && ('fail') te && te('fail') } (); };// input⾃带的select()⽅法在苹果端⽆法进⾏选择,所以需要⾃⼰去写⼀个类似的⽅法 // 选择⽂本。createTextRange(setSelectionRange)是input⽅法 const selectText = (textbox, startIndex, stopIndex)=> { if (TextRange) {//ie const range = TextRange(); se(true); art('character', startIndex);//起始光标 d('character', stopIndex - startIndex);//结束光标 ();//不兼容苹果 } else {//firefox/chrome ectionRange(startIndex, stopIndex); (); } } // 复制⽂字 // 必须⼿动触发 点击事件或者其他事件,不能直接使⽤js调⽤// copyText('h5实现⼀键复制到粘贴板 兼容ios') /*兼容性补充: 移动端: 安卓⼿机:微信(chrome)和⼏个⼿机浏览器都可以⽤。 苹果⼿机:微信⾥⾯和sarafi浏览器⾥也都可以, PC:sarafi版本必须在10.2以上,其他浏览器可以. 兼容性测试⽹站:/*/export default { setClipboardData}
发布评论