Clipboard 接口的 writeText() property writes the specified text string to the system clipboard. Text may be read back using either read() or readText() .

"clipboard-write" permission of the 权限 API , is granted automatically to pages when they are in the active tab.

注意: Browser support for the asynchronous clipboard APIs is still in the process of being implemented. Be sure to check the 兼容性表格 Clipboard availability in Clipboard 了解更多信息。

句法

var promise = navigator.clipboard.writeText(newClipText)
					

参数

newClipText
DOMString to be written to the clipboard.

返回值

A Promise which is resolved once the clipboard's contents have been updated. The promise is rejected if the caller does not have permission to write to the clipboard.

范例

This example sets the clipboard's contents to the string "<empty clipboard>".

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

规范

规范 状态 注释
Clipboard API and events
The definition of 'writeText()' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
writeText Chrome 66 Edge 79 Firefox 63
63
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 Opera 53 Safari 13.1 WebView Android 66 Chrome Android 66 Firefox Android 63
63
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 47 Safari iOS 13.4 Samsung Internet Android 9.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. Clipboard
  2. 方法
    1. read()
    2. readText()
    3. write()
    4. writeText()
  3. 继承:
    1. EventTarget
  4. Related pages for Clipboard API
    1. ClipboardEvent
    2. ClipboardItem
    3. Navigator.clipboard

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1