if (/iphone/.test(window.navigator.userAgent.toLowerCase())) {
const el = document.createElement('input');
const btn = document.getElementById('btn');
el.value = this.state.coupon;
el.style.opacity = '0';
document.body.appendChild(el);
const editable = el.contentEditable;
const readOnly = el.readOnly;
el.contentEditable = true;
el.readOnly = false;
const range = document.createRange();
range.selectNodeContents(el);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
el.setSelectionRange(0, 999999);
el.contentEditable = editable;
el.readOnly = readOnly;
const ret = document.execCommand('copy');
el.blur();
//
const msg = ret? '複製成功': '複製失敗'
alert(msg)
} else {
var copyText = document.getElementById("copy");
copyText.contentEditable = true;
copyText.readonly = false;
copyText.focus();
copyText.select();
alert('複製成功!')
/* Copy the text inside the text field */
document.execCommand("copy");
}
參考
https://nodefe.com/ios-js-copy/
留言
張貼留言