From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/clipboard/write/index.html | 76 ++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 files/zh-cn/web/api/clipboard/write/index.html (limited to 'files/zh-cn/web/api/clipboard/write') diff --git a/files/zh-cn/web/api/clipboard/write/index.html b/files/zh-cn/web/api/clipboard/write/index.html new file mode 100644 index 0000000000..a7f69da118 --- /dev/null +++ b/files/zh-cn/web/api/clipboard/write/index.html @@ -0,0 +1,76 @@ +--- +title: Clipboard.write() +slug: Web/API/Clipboard/write +translation_of: Web/API/Clipboard/write +--- +

{{APIRef("Clipboard API")}}

+ +
 
+ +

 

+ +
 
+ +

{{domxref("Clipboard")}} 的方法 write() 写入图片等任意的数据到剪贴板。 这个方法可以用于实现剪切和复制的功能。

+ +

但是你要提前获取 "Permissions API" 的 "clipboard-write" 权限才能将数据写入到剪贴板。

+ +
+

注意: 浏览器对这个异步剪贴板的 API 仍然在讨论中。所以在使用它之前请检查 {{anch("Browser compatibility", "compatibility table")}} 和 {{SectionOnPage("/en-US/docs/Web/API/Clipboard", "Clipboard availability")}} 以获得更多的兼容性信息。

+
+ +

语法

+ +
var promise = navigator.clipboard.write(dataTransfer)
+ +

参数

+ +
+
dataTransfer
+
{{domxref("DataTransfer")}} 对象包含了要写入剪贴板的数据。
+
+ +

返回值

+ +

当数据被写入到剪贴板的时候,{{jsxref("Promise")}} resolve 回调被执行。如果剪贴板不能完成剪贴操作,{{jsxref("Promise")}}  reject 回调被执行。

+ +

示例

+ +

这个例子展示了如何将当前剪贴板的内容替换为给定的内容。

+ +
function setClipboard(text) {
+  let data = new DataTransfer();
+
+  data.items.add("text/plain", text);
+  navigator.clipboard.write(data).then(function() {
+    /* success */
+  }, function() {
+    /* failure */
+  });
+}
+
+ +

代码创建了一个 {{domxref("DataTransfer")}} 对象,要替换的内容存储在这里。执行 {{domxref("DataTransferItemList.add()")}} 将数据写入进去,然后执行 write() 方法,指定执行成功或错误的结果。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Clipboard API','#h-clipboard-write-data','write()')}}{{Spec2('Clipboard API')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Clipboard.write")}}

-- cgit v1.2.3-54-g00ecf