赞
踩
项目经常会出现复制粘贴,拷贝一段文字/手机号/兑换码,每次重新写比较麻烦,记录一下,方便以后调用
- webCopyString(str: string) {
- var input = str + '';
- const el = document.createElement('input');
- el.value = input;
- el.setAttribute('readonly', '');
- el.style.contain = 'strict';
- el.style.position = 'absolute';
- el.style.left = '-100vw';
- el.style.fontSize = '12px'; // Prevent zooming on iOS
- document.body.appendChild(el);
- el.select();
- el.selectionStart = 0;
- el.selectionEnd = input.length;
- var success = false;
- try {
- success = document.execCommand('copy');
- } catch (err) { }
-
- document.body.removeChild(el);
- return success;
- }
- onCopyCode(copyTxt) {
- const success = this.webCopyString(copyTxt);
- if (success) {
- alert('复制成功');
- } else {
- alert('复制失败');
- }
- console.log('success', success);
- }
使用方式 调用函数onCopyCode("需要复制的内容");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。