Clipboard 方法 write() writes arbitrary data, such as images, to the clipboard. This can be used to implement cut and copy functionality.

"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.write(data)
					

参数

data
An array of ClipboardItem objects containing data to be written to the clipboard.

返回值

A Promise which is resolved when the data has been written to the clipboard. The promise is rejected if the clipboard is unable to complete the clipboard access.

范例

This example function replaces the current contents of the clipboard with a specified string.

function setClipboard(text) {
  let data = [new ClipboardItem({ "text/plain": text })];
  navigator.clipboard.write(data).then(function() {
    /* success */
  }, function() {
    /* failure */
  });
}
					

The code begins by creating a new ClipboardItem object into which the text will be placed for sending to the clipboard. The key of the object passed to the ClipboardItem constructor indicates the content type, the value indicates the content. The content could be a text or even a Blob (e.g. for copying images to the clipboard). Then write() is called, specifying both a fulfilment function and an error function.

Example of copying canvas contents to the clipboard

function copyCanvasContentsToClipboard(canvas, onDone, onError) {
  canvas.toBlob(function (blob) {
    let data = [new ClipboardItem({ [blob.type]: blob })];
    navigator.clipboard.write(data).then(function () {
      onDone();
    }, function (err) {
      onError(err);
    })
  });
}
					

注意 : You can only pass in one clipboard item at a time.

规范

规范 状态 注释
Clipboard API and events
The definition of 'write()' 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
write Chrome 66
66
From version 76, the image/png MIME type is supported.
Edge 79 Firefox 63 Disabled
63 Disabled
Currently works exactly like writeText() , including the availability limitations currently imposed by Firefox.
Disabled ). To change preferences in Firefox, visit about:config.
IE No Opera 63 Safari 13.1 WebView Android 66
66
From version 84, the image/png MIME type is supported.
Chrome Android 66
66
From version 84, the image/png MIME type is supported.
Firefox Android 63 Disabled
63 Disabled
Currently works exactly like writeText() , including the availability limitations currently imposed by Firefox.
Disabled ). To change preferences in Firefox, visit about:config.
Opera Android 54 Safari iOS 13.4 Samsung Internet Android 12.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