Skip to content

Instantly share code, notes, and snippets.

@zymiboxpay
Created February 7, 2017 03:23
Show Gist options
  • Save zymiboxpay/6a4b26eeff4e97a55ae53c51c7f5fb65 to your computer and use it in GitHub Desktop.
Save zymiboxpay/6a4b26eeff4e97a55ae53c51c7f5fb65 to your computer and use it in GitHub Desktop.
copy_to_clipboard
(function(global, undefined){
function copyToClipboard(content, successCallback) {
successCallback = successCallback || alert.bind(this, '复制成功');
// http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
// IE 8
if(window.clipboardData) {
window.clipboardData.setData('Text', content);
e.preventDefault();
}
// IE 9 以及现代浏览器
else {
var textArea = document.createElement('textarea');
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '1px';
textArea.style.height = '1px';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
document.body.appendChild(textArea);
textArea.value = content;
textArea.select();
try {
var successful = document.execCommand('copy');
if(successful){
successCallback();
}
else {
prompt("请按 Ctrl+C 复制内容", content);
}
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
}
global.copyToClipboard = copyToClipboard;
})(window, undefined);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment