static copyToClipBoard(str) :boolean {
if (cc.sys.isNative) {
//原生自己实现
} else if (cc.sys.isBrowser) {
var textarea = document.createElement("textarea");
textarea.textContent = str;
document.body.appendChild(textarea);
textarea.readOnly = true;
textarea.select();
textarea.setSelectionRange(0, textarea.textContent.length);
try {
const flag = document.execCommand('copy');
document.body.removeChild(textarea);
if(flag){
cc.log("已经复制到剪贴板");
return true;
}else{
cc.log("复制到剪贴板失败");
return false;
}
} catch (err) {
cc.log("复制到剪贴板失败");
return false;
}
}
}
需要注意的是在部分机器中复制功能需要由用户的点击事件来触发复制功能