button οnclick={onClick()}>点击复制button>
// 复制
const onClick = (item, value) => {
var data='sssssssss'
if(navigator.clipboard && window.isSecureContext){
navigator.clipboard.writeText(data).then(function () {
}, function () {
});
}else{
let textArea = document.createElement("textarea");
textArea.value = data;
// 使text area不在viewport,同时设置不可见
textArea?.style?.position = "absolute";
textArea?.style?.opacity = 0;
textArea?.style?.left = "-999999px";
textArea?.style?.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
// 执行复制命令并移除文本框
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
}
navigator.clipboard.writeText的方法在测试环境不能用时就用else的方法