Last active
March 13, 2017 06:21
-
-
Save wilon/4687967079fe42e1fd7c8cc257c2abeb to your computer and use it in GitHub Desktop.
JavaScript将文本复制到剪切板函数
This file contains hidden or 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
String.prototype.copyToClipboard = function() { | |
var textArea = document.createElement("textarea"); | |
textArea.value = this; | |
document.body.appendChild(textArea); | |
textArea.select(); | |
try { | |
var result = document.execCommand('copy'); | |
} catch (e) { | |
var result = false; | |
} | |
document.body.removeChild(textArea); | |
console.log(`'${this}' copyTextToClipboard: ${result}`); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JavaScript将文本复制到剪切板函数
Useage:
'2333333'.copyToClipboard();
Then try
Paste
Console:
'233333333' copyTextToClipboard: true