//复制文本到剪切板
copyText(val){
var input = document.createElement('input');
input.setAttribute('readonly', 'readonly'); // 防止手机上弹出软键盘
input.setAttribute('value',val);
document.body.appendChild(input);
input.select();
var res = document.execCommand('copy');
document.body.removeChild(input);
return res;
}
这段代码演示了如何利用JavaScript创建一个输入元素,设置其值为指定文本,选中该值,然后执行复制命令,最后移除这个临时的输入元素。此方法常用于实现网页上的文本复制功能。

被折叠的 条评论
为什么被折叠?



