uniapp 兼容H5复制文本功能,亲测可用

封装copyText函数,具体如下:

copyText(val){
    let result
    // #ifndef H5
    uni.setClipboardData({
      data: val,
      success() {
        result = true
      },
      fail() {        
        result = false        
      }
    });
    // #endif
    
    // #ifdef H5 
      let textarea = document.createElement("textarea")
      textarea.value = val
      textarea.readOnly = "readOnly"
      document.body.appendChild(textarea)
      textarea.select() // 选中文本内容
      textarea.setSelectionRange(0, val.length) 
      result = document.execCommand("copy") 
      textarea.remove()    
    // #endif
      return result
  },

 

posted @ 2020-11-12 17:00  恨钱不成山  阅读(2581)  评论(0)    收藏  举报