/**
* 复制到剪切板,支持换行文本
* @param String value 需要复制的内容
*/
function copy(value) {
const text = document.createElement('textarea');
text.value = value;
text.setAttribute('readonly', 'readonly')
document.body.appendChild(text);
text.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
}
document.body.removeChild(text);
}
// 示例一:单行文本
copy('哈哈哈')
// 示例二:多行文本
copy('姓名:张三\n性别:男\n年龄:30')
// 示例三:多行文本可以用ES6的模板拼接
copy(`
姓名:张三
性别:男
年龄:30
`)
JS复制到剪切板、支持多行文本复制、解决移动端唤起软键盘
最新推荐文章于 2024-09-26 19:24:27 发布