在做項目的過程中,為了完善用戶體驗,嘗嘗需要做復(fù)制文本的功能。
而 uni-app官方提供的uni.setClipboardData(OBJECT)復(fù)制功能并不兼容H5端,
所以我們要做個區(qū)分,經(jīng)過本人測試,可以使用,直接上代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-521490.html
let text = '所要復(fù)制的內(nèi)容'
// #ifdef H5
let textarea = document.createElement("textarea")
textarea.value = text
textarea.readOnly = "readOnly"
document.body.appendChild(textarea)
textarea.select() // 選中文本內(nèi)容
textarea.setSelectionRange(0, text.length)
uni.showToast({//提示
title:'復(fù)制成功' ,
icon:'success'
})
document.execCommand("copy")
textarea.remove()
// #endif
// #ifndef H5
uni.setClipboardData({
data:text,//要被復(fù)制的內(nèi)容
success:()=>{//復(fù)制成功的回調(diào)函數(shù)
uni.showToast({//提示
title:`復(fù)制成功`,
icon:'success'
})
}
},true);
// #endif
文章來源:http://www.zghlxwxcb.cn/news/detail-521490.html
到了這里,關(guān)于uni-app實現(xiàn)復(fù)制文本的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!