记得很早的时候实现过一次,只不过那次是 纯 vue 实现的 h5+,但是想用在 uniapp中 还是不行。有些地方不能用!不过在 App 中的方法还是可以使用的:

Vue 复制内容到系统剪切板_唐僧的专栏-CSDN博客_paste vueVue 复制内容到系统剪切板板在具体生产环境中,经常会遇到将内容复制到剪切板的 使用情况。由于我们项目是 h5+ 的项目。会打包成App;有时候在网页版中使用该功能是OK的,但是 发布成APP 在 IOS中功能就不好使了。在这里我们 将 复制到剪切板 功能 细分为 在Web中 和APP中两种情况来...https://blog.csdn.net/nicepainkiller/article/details/90667650?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164274964116780271535911%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164274964116780271535911&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-90667650.nonecase&utm_term=%E5%89%AA%E5%88%87%E6%9D%BF.&spm=1018.2226.3001.4450

uniapp 复制到剪切板 android  ios   android-web  ios-web

这里是实现了一个长按复制功能:后来发现 有些手机的自带浏览器有冲突 后来改为点击复制了(oppo 自带的浏览器就会出这个问题)

这里使用了 卡槽的设计,只要有需要的复制的 控件 直接试用卡槽包裹 就可以使用

longPressCopy.vue

<template><!--  <view class="long-press-copy" @touchstart="onTouchStart" @touchend="onTouchEnd"> --><view class="long-press-copy" @click="onTimerUp()"><slot name="contentBox"></slot></view>
</template><script>export default {data() {return {timeOut: 700,time: 0,timerCount: null}},props: {info: {}},methods: {onTouchStart() {this.time = (new Date()).getTime()if (this.timerCount) {clearInterval(this.timerCount)this.timerCount = null}this.timerCount = setInterval(this.onTimer, 100)},onTouchEnd() {if (this.timerCount) {clearInterval(this.timerCount)this.timerCount = null}},onTimer() {if (((new Date()).getTime() - this.time) >= this.timeOut) {this.time = (new Date()).getTime()this.onTimerUp()if (this.timerCount) {clearInterval(this.timerCount)this.timerCount = null}}},onTimerUp() {// #ifdef H5if (this.copyToClipboard(this.info)) {uni.showToast({title: 'Copied',position: 'bottom',duration: 1500,icon: 'none'})console.log('ok')} else {console.log('no ok')}// #endif// #ifdef APP-PLUSconst os = plus.os.nameif (os === 'iOS') {var pasteboard = plus.ios.invoke('UIPasteboard', 'generalPasteboard')plus.ios.invoke(pasteboard, 'setValue:forPasteboardType:', this.info, 'public.utf8-plain-text')} else {var main = plus.android.runtimeMainActivity()var clip = main.getSystemService('clipboard')plus.android.invoke(clip, 'setText', this.info)}uni.showToast({title: 'Copied',position: 'bottom',duration: 1500})// #endif},copyToClipboard(string) {let textarealet resulttry {textarea = document.createElement('textarea')textarea.setAttribute('readonly', true)textarea.setAttribute('contenteditable', true)textarea.style.position = 'fixed' // prevent scroll from jumping to the bottom when focus is set.textarea.value = stringdocument.body.appendChild(textarea)textarea.focus()textarea.select()const range = document.createRange()range.selectNodeContents(textarea)const sel = window.getSelection()sel.removeAllRanges()sel.addRange(range)textarea.setSelectionRange(0, textarea.value.length)result = document.execCommand('copy')} catch (err) {console.error(err)result = null} finally {document.body.removeChild(textarea)}// manual copy fallback using promptif (!result) {const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0const copyHotkey = isMac ? '⌘C' : 'CTRL+C'result = prompt(`Press ${copyHotkey}`, string) // eslint-disable-line no-alertif (!result) {return false}}return true},destroyed() {if (this.timerCount) {clearInterval(this.timerCount)this.timerCount = null}}}}
</script><style scoped lang="scss">.long-press-copy {}
</style>

具体使用的地方:

<long-press :info="goodInfo.spuCode"><view class="code-value" slot="contentBox">{{goodInfo.spuCode}}</view>
</long-press>

经过测试四端 都能使用

uniapp 复制到剪切板 四端适配相关推荐

  1. html数据复制到剪切板

    一.原生js指令复制 function copyUtil(info) {var $textArea = $('<textarea></textarea>');$textArea ...

  2. 微信小程序复制到剪切板及换行问题

    wxml <textarea type="text" v-model="copyContent" placeholder="请输入内容" ...

  3. clipboard.js 实现动态获取内容并复制到剪切板

    一个按钮实现先ajax请求,再实现复制功能: 使用clipboard.js分为以下几个步骤: 1.引入一个clipboard.js的文件: 2.新建一个clipboard对象: 3.点击按钮获取目标对 ...

  4. 将内容复制到剪切板兼容主流浏览器的解决方案

    html : <body><div class="demo-area"><label for="copy-input">输入 ...

  5. 微信小程序_把chatgpt聊天数据复制到剪切板

    文章目录 ⭐ 前言 ⭐ 开始 网格背景样式配置 对话框样式配置 复制到剪切板 ⭐ 结束 ⭐ 前言 大家好,我是yma16,不止前端,本文将介绍微信小程序中 chatgpt聊天页面设计和复制聊天数据. ...

  6. 微信小程序之一键复制到剪切板

    最近在开发小程序项目里遇到一个小难题-----一键复制 在网页开发里 我们可以引用大牛封装好的复制插件,比如无flash插件clipboard.js适用移动端,pc端------- 言归正装,打开微信 ...

  7. Android将内容复制到剪切板

    近期,项目里需要用到将部分内容复制到剪切板,以前用到都是随用随找,挺麻烦的,最近整理一下,在这里记录一下 其实Android提供的剪贴板框架,Android提供ClipboardManager.Cli ...

  8. Html将内容复制到剪切板.

    Html将内容复制到剪切板. 引入clipboard.min.js /*!* clipboard.js v1.6.1* https://zenorocha.github.io/clipboard.js ...

  9. Web功能之复制粘贴剪切板

    目录 一.展示 1.代码展示 2.实际效果展示 二.具体实现 简述步骤 三.疑惑与解答 一.展示 1.代码展示 <!DOCTYPE html> <html> <head& ...

最新文章

  1. 正向最大匹配 和逆向最大匹配对比比较
  2. mongodb 设置远程可以访问
  3. boost::signals2::trackable相关的测试程序
  4. 能干的产品经理比不上能说的产品经理
  5. python3.5安装scrapy_win7+Python3.5下scrapy的安装方法
  6. 弹性分布式数据集RDD
  7. 面试题系列(三)-socket
  8. httpwatch使用_使用JavaScript的HTTPWatch自动化
  9. error: You have not concluded your merge (MERGE_HEAD exists).
  10. java图片双缓存_Java 双缓冲技术消除图片闪动
  11. linux7.5开放端口,Centos/linux开放端口
  12. Java生成随机数并随机输出不重复的值
  13. Sql语句四舍五入的几种方法
  14. 数据归约——主成分分析PCA
  15. scau 8639 折半插入排序
  16. 整型数据类型有哪些?有哪些表现形式?
  17. UGUI实现虚拟手柄功能
  18. 航天战场态势感知平台
  19. WordPress网站创建和添加Google数据分析代码完整指南(2022年8月最新)
  20. blueStacks模拟器竖屏调整

热门文章

  1. 计算机体系结构分类-Flynn分类法
  2. Kubernetes 是什么 ?
  3. 认证AAA企业信用评级,对公司未来发展如何?
  4. [转]Markdown 语法说明 (简体中文版)
  5. 量子面板 管理多个青龙+对接公众号+QQ机器人+多面板管理+VLW
  6. Secret、ConfigMap
  7. iOS越狱开发之MobileSubstrate介绍
  8. Linux应用程序开发:进程的一些事儿
  9. 解决Go无法下载依赖问题 i/o timeout
  10. windows的USB插拔事件日志