function copyToClipboard(text) {
if (text.indexOf('-') !== -1) {
let arr = text.split('-');
text = arr[0] + arr[1];
}
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = '0';
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? '成功复制到剪贴板' : '该浏览器不支持点击复制到剪贴板';
alert(msg);
} catch (err) {
alert('该浏览器不支持点击复制到剪贴板');
}
document.body.removeChild(textArea);
}
js把文本复制到剪切版,实现一键复制内容功能
未经允许不得转载:自游互动 » js把文本复制到剪切版,实现一键复制内容功能
相关推荐
- h5页面解决ios中safari浏览器伪元素hover点击两次的问题
- js设置光标始终在文本最后
- ie中sort提示语法错误, ie中无效的解决方案
- 解决jquery在ie下ajax post 提交时的乱码问题
- vue.js在html绑定click时绑定父组件方法
- 外部调用vue对象实例内属性、方法、计算属性
- Safari中touchstart的点击事件不存在 event.path 的兼容方法
- xpath在class有多个时的查找方法
- 微信小程序生成二维码工具weapp-qrcode.js类库
- js通过history.pushState实现改变url不刷新页面
- JQuery+ajax+jsonp +.net core3.0返回jsonp数据解决跨域问题
- 微信小程序、js数组让元素置顶,置底
- 微信小程序、js四舍五入并保留两位小数
- 微信小程序js实现数组元素上下移动
- js 一键复制文本到剪切板
- Vue.js 监控v-for循环渲染完成后再执行方法