赞
踩
封装一个方法,在自己全局的js里,调用起来也方便
- function copyTxt(txt) {//传入要复制的内容
- txt+="";
- if(txt=="null"||txt=="undefined"||txt==""){
- //toast("复制失败,内容为空");
- return;
- }
- // #ifdef APP-PLUS
- uni.setClipboardData({
- data:txt,
- });
- // #endif
-
- // #ifdef H5
- if (document.queryCommandSupported('copy')) {
- let textarea = document.createElement("textarea")
- textarea.value = txt
- textarea.readOnly = "readOnly"
- document.body.appendChild(textarea)
- textarea.select() // 选中文本内容
- textarea.setSelectionRange(0, txt.length)
- let result = document.execCommand("copy")
- textarea.remove()
- toast("复制成功");
- }else{
- toast("您的浏览器不支持复制");
- }
-
- // #endif
- }
- export default {
- copyTxt,
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。