1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| function copy() { if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { console.log('ios')
window.getSelection().removeAllRanges();
var copyNode = document.getElementById("copy"); var editable = copyNode.contentEditable; var readOnly = copyNode.readOnly; copyNode.contentEditable = true; copyNode.readOnly = true;
var range = document.createRange(); range.selectNodeContents(copyNode);
var selection = window.getSelection(); if (selection.rangeCount > 0) { selection.removeAllRanges(); }
selection.addRange(range); copyNode.setSelectionRange(0, 999999); copyNode.contentEditable = editable; copyNode.readOnly = readOnly;
var successful = document.execCommand('copy');
console.log('successful = ', successful); if (successful) { layer.open({ content: '复制成功^_^', btn: ['好的'], yes: function (index) { layer.closeAll(); } }) } window.getSelection().removeAllRanges(); } else { var text = document.getElementById("copy").value; const textarea = document.createElement("textarea"); textarea.style.position = 'fixed'; textarea.style.top = 0; textarea.style.left = 0; textarea.style.border = 'none'; textarea.style.outline = 'none'; textarea.style.resize = 'none'; textarea.style.fontSize = '12pt'; textarea.style.background = 'transparent'; textarea.style.color = 'transparent'; textarea.value = text; document.body.appendChild(textarea); textarea.select() try { const msg = document.execCommand('copy') ? 'successful' : 'unsuccessful'; if (msg == 'successful') { layer.open({ content: '复制成功^_^', btn: ['好的'], yes: function (index) { layer.closeAll(); } }) } } catch (err) { alert('unable to copy', err) } document.body.removeChild(textarea) }
}
|