Skip to content

Instantly share code, notes, and snippets.

@DesignHhuang
Created September 28, 2023 10:47
Show Gist options
  • Save DesignHhuang/43a41a1691041e8f1c51df0f3aa9a312 to your computer and use it in GitHub Desktop.
Save DesignHhuang/43a41a1691041e8f1c51df0f3aa9a312 to your computer and use it in GitHub Desktop.
useCopyToClipboard:使用复制到剪贴板
function copyToClipboard(text) {
let input = document.createElement('input');
input.setAttribute('value', text);
document.body.appendChild(input);
input.select();
let result = document.execCommand('copy');
document.body.removeChild(input);
return result;
}
export const useCopyToClipboard = () => {
return (text) => {
if (typeof text === "string" || typeof text == "number") {
return copyToClipboard(text);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment