uni-app 复制API setClipboardData 在H5端不支持
·
解决在uni-app中使用 uni.setClipboardData 复制文本,但却在H5端不起作用的问题
function copyText(content) {
// 不支持复制数字类型所以需要先转为字符串
content = typeof content === 'string' ? content : content.toString()
//#ifndef H5
uni.setClipboardData({
data: content,
success: function() {
console.log('success');
}
});
//#endif
// #ifdef H5
if (!document.queryCommandSupported('copy')) {
uni.showToast({
title: '浏览器不支持',
icon: 'none',
duration: 2000
});
return;
}
let textarea = document.createElement("textarea")
textarea.value = content
textarea.readOnly = "readOnly"
document.body.appendChild(textarea)
textarea.select() // 选择对象
textarea.setSelectionRange(0, content.length) //核心
let result = document.execCommand("copy") // 执行浏览器复制命令
if (result) {
uni.showToast({
title: '内容已复制',
duration: 2000
});
}
textarea.remove()
// #endif
}
更多推荐

所有评论(0)