当前位置:网站首页>微信小程序wx.setClipboardData复制文本
微信小程序wx.setClipboardData复制文本
2022-07-17 05:05:00 【小青年一枚】
今天在开发中遇到一个问题,在小程序中 复制文本,在做之前也查了资料,大部分都是固定文本复制,(小程序本身是不能手动复制的)和需求还是不一样的。
对于复制官方文档中提供了:
wx.setClipboardData设置系统剪贴板的内容。调用成功后,会弹出 toast 提示"内容已复制",持续 1.5s。
方法一:要求在小程序页面中可以手动选择文本复制。
<text selectable="true">{ {texts}}</text>
在text标签中添加 selectable="true" 属性就可以了。
方法二:当复制一段文字或者一行文字的时候。
<view wx:for="{
{newArr}}" wx:key="index">
<text class="modal-li" selectable="true">{
{item.title}}</text>
<view class="iconfont icon-fuzhi" bindtap="copyBtn" data-title="{
{item.title}}">复制</view>
</view>copyBtn(){
let that = this;
wx.setClipboardData({//复制文本
data: that.data.newArr,
success: function (res) {
wx.showToast({
title: '复制成功',
icon:"none",
mask:"true"//是否设置点击蒙版,防止点击穿透
})
}
})
}在复制按钮中添加 data-属性="{ {item.属性}}" 就可以实现复制。
或者 当遇到复制手机号的时候也可以用这种方法来实现。
<text class="modal-li" selectable="true">{
{title}}</text>
<view class="iconfont icon-fuzhi" bindtap="copyBtn" data-title="{
{title}}">复制</view>
方法三:当循环出列表数据,点击复制,复制全部数据。这样就不能用data-了。如果用的话就是相当于一个li标签中有一个复制按钮,感觉用户体验也不好,也没达到需求。
<view class="iconfont icon-fuzhi" bindtap="copyBtn" >复制</view>
<view wx:for="{
{newArr}}" wx:key="index">
<text class="modal-li" selectable="true">{
{item.title}}</text>
</view>copyBtn() {
let that=this;
let result = "";
let newcopy = that.data.newArr;
for(var i = 0; i < newcopy.length; i++){
console.log('遍历打印==', newcopy[i].title);
wx.setClipboardData({//复制文本
data: result += newcopy[i].title + "\n",
success: function (res) {
wx.showToast({
title: '复制成功',
icon:"none",
mask:"true"//是否设置点击蒙版,防止点击穿透
})
}
})
};首先遍历数组,其次生命一个变量,把数组拼接字符串。这样就可以复制全部的数据。
第一次接触小程序复制的功能,不足之处欢迎指正。
边栏推荐
- 决策树原理和案例应用-泰坦尼克号生存预测
- First training notes of moderlarts
- Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)
- 这么6的刷题网站你不会没听说过吧?你已经out 了?
- 【C语言—零基础第十课】数组王国奇遇记
- 读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》
- Notes de formation pour la deuxième fois des modèles
- 645. 错误的集合
- POC——DVWA‘s XSS Reflected
- 硬核结构体,暴力解读
猜你喜欢

One article to understand Zipkin

PyGame installation -requirement already satisfied

Teddy Cup title a full version optimization update (4/23)

CVE-2019-14234 Django JSONField SQL注入漏洞

Asynchronous data SMS verification code

一个问题的探讨

POC——DVWA‘s SQL Injection
[email protected] : `node install. Problems of js`"/>solve [email protected] : `node install. Problems of js`

【C】 Beam calculator

用户登录-以及创建验短信证码
随机推荐
关于当前响应已经调用了方法getOutputStream()
About the current response, the method getoutputstream() has been called
学习C语言第8天
一个问题的探讨
. SH scripting
Infinite classification
ModelArts第二次培訓筆記
学习C语言第三天
Shallow chat link tracking
数据库实训7【索引与数据完整性约束的创建】
【Batch】批量删除中间文件夹-个人研究脚本
Install MySQL
Pygame:外星人入侵
卷积神经网络
MySQL fuzzy matching 1, 11111 similar string problems
【p5.js】模拟烟花效果-交互媒体设计作业
User management - restrictions
md5 密码加密
C语言练习2
Learn about scheduled tasks in one article