复制文本。
现代浏览器优先使用 navigator.clipboard.writeText;不可用时退回到隐藏 textarea 和 document.execCommand('copy')。 如果运行环境没有 Promise,需要调用方自行提供 Polyfill。
navigator.clipboard.writeText
document.execCommand('copy')
被复制的文本
文本写入剪贴板后的 Promise
0.0.21
(async () => { const text = await copyText('hello world'); // 此时按 Ctrl / Command + V 即可粘贴「hello world 文本」} Copy
(async () => { const text = await copyText('hello world'); // 此时按 Ctrl / Command + V 即可粘贴「hello world 文本」}
复制文本。
现代浏览器优先使用
navigator.clipboard.writeText;不可用时退回到隐藏 textarea 和document.execCommand('copy')。 如果运行环境没有 Promise,需要调用方自行提供 Polyfill。