js复制文字的方法

function copyText(text){
var textValue = document.createElement('textarea');
textValue.setAttribute('readonly', 'readonly'); //设置只读属性防止手机上弹出软键盘
textValue.value = text;
document.body.appendChild(textValue); //将textarea添加为body子元素
textValue.select();
var res = document.execCommand('copy');
document.body.removeChild(textValue);//移除DOM元素
console.log("复制成功");
return res;
}

js复制文字的方法

posted @ 2023-03-15 16:27  newmiracle宇宙  阅读(184)  评论(0编辑  收藏  举报