Skip to content

Instantly share code, notes, and snippets.

@wilon
Last active March 13, 2017 06:21
Show Gist options
  • Save wilon/4687967079fe42e1fd7c8cc257c2abeb to your computer and use it in GitHub Desktop.
Save wilon/4687967079fe42e1fd7c8cc257c2abeb to your computer and use it in GitHub Desktop.
JavaScript将文本复制到剪切板函数
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;
};
@wilon
Copy link
Author

wilon commented Mar 13, 2017

JavaScript将文本复制到剪切板函数

  • Useage:
    '2333333'.copyToClipboard();
    Then try Paste

  • Console:
    '233333333' copyTextToClipboard: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment