Created
April 18, 2018 05:55
-
-
Save 599316527/714baef78ef430ea3de2e1617a266586 to your computer and use it in GitHub Desktop.
Pure JS copy text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function copyText(value) { | |
let fakeElem = document.createElement('TEXTAREA') | |
fakeElem.style.position = 'absolute' | |
fakeElem.style.left = '-9999px' | |
fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px' | |
fakeElem.readonly = true | |
fakeElem.value = value | |
document.body.appendChild(fakeElem) | |
fakeElem.focus() | |
fakeElem.setSelectionRange(0, value.length) | |
try { | |
document.execCommand('copy') | |
} catch (err) { | |
console.log('不支持复制: ' + err.message) | |
} | |
fakeElem.blur() | |
document.body.removeChild(fakeElem) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment