Clipboard.writeText()

Clipboard 接口的 writeText() 方法可以写入特定字符串到操作系统的剪切板。

Note: 规范要求在写入剪贴板之前使用 Permissions API 获取“剪贴板写入”权限。但是,不同浏览器的具体要求不同,因为这是一个新的API。有关详细信息,请查看compatibility table and Clipboard availability in Clipboard

语法

var promise = navigator.clipboard.writeText(newClipText)

参数

newClipText
The DOMString to be written to the clipboard.

返回值

一个Promise ,一旦剪贴板的内容被更新,它就会被解析。如果调用者没有写入剪贴板的权限,则拒绝写入剪切板(reject)

例子

此示例将剪贴板的内容设置为字符串“<empty clipboard>”。

navigator.clipboard.writeText("<empty clipboard>").then(function() {
  /* clipboard successfully set */
}, function() {
  /* clipboard write failed */
});

规范

规范 状态 注解
Clipboard API and events
writeText()
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
writeTextChrome Full support 66Edge No support NoFirefox Full support 63
Notes
Full support 63
Notes
Notes Writing to the clipboard is available without permission in secure contexts and browser extensions, but only from user-initiated event callbacks. Browser extensions with the "clipboardWrite" permission can write to the clipboard at any time.
IE No support NoOpera Full support 53Safari ? WebView Android No support NoChrome Android Full support 66Firefox Android Full support 63
Notes
Full support 63
Notes
Notes Writing to the clipboard is available without permission in secure contexts and browser extensions, but only from user-initiated event callbacks. Browser extensions with the "clipboardWrite" permission can write to the clipboard at any time.
Opera Android Full support 47Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.