function copyText(str) {
let ios = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
let Url2 = str || '';
let oInput = document.createElement('input');
oInput.className = 'oInput';
oInput.id= 'copyInput';
oInput.value = Url2;
document.body.appendChild(oInput);
if (ios) {
let range = document.createRange();
range.selectNode(document.querySelector(".oInput"));
let selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
} else {
oInput.select(); // 选择对象
}
document.execCommand("Copy"); // 执行浏览器复制命令
oInput.style.display='none';
}
如果事件存在于ng-repeat中,那么只有第一个复制是成功的。
解决办法:每次触发事件时,先删除掉生成的input。然后再重新生成。
//放在上方函数的开头
if(documnet.getElementById('CopyInput')){
document.body.removeChild(documnet.getElementById('CopyInput'));
}