#复制到剪贴板
随笔记录一段兼容安卓IOS的复制到剪贴板功能代码
<textarea id="couponStr" style="opacity: 0;">需复制的内容</textarea>
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
window.getSelection().removeAllRanges();
var couponStr = document.getElementById("couponStr");
var range = document.createRange();
range.selectNode(couponStr);
window.getSelection().addRange(range);
var successful = document.execCommand('copy');
// 移除选中的元素
window.getSelection().removeAllRanges();
} else {
var save = function (e) {
e.clipboardData.setData('text/plain', '需复制的内容');
e.preventDefault();
}
document.addEventListener('copy', save);
document.execCommand('copy');
document.removeEventListener('copy', save);
}