diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/clipboard/write | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/clipboard/write')
-rw-r--r-- | files/zh-cn/web/api/clipboard/write/index.html | 76 |
1 files changed, 76 insertions, 0 deletions
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 +--- +<p>{{APIRef("Clipboard API")}}</p> + +<div> </div> + +<p> </p> + +<div> </div> + +<p><span class="seoSummary">{{domxref("Clipboard")}} 的方法 <strong><code>write()</code></strong> 写入图片等任意的数据到剪贴板。</span> 这个方法可以用于实现剪切和复制的功能。</p> + +<p>但是你要提前获取 "<a href="/en-US/docs/Web/API/Permissions_API">Permissions API</a>" 的 <code>"clipboard-write"</code> 权限才能将数据写入到剪贴板。</p> + +<div class="note"> +<p><strong>注意:</strong> 浏览器对这个异步剪贴板的 API 仍然在讨论中。所以在使用它之前请检查 {{anch("Browser compatibility", "compatibility table")}} 和 {{SectionOnPage("/en-US/docs/Web/API/Clipboard", "Clipboard availability")}} 以获得更多的兼容性信息。</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>promise</em> = navigator.clipboard.write(<em>dataTransfer</em>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>dataTransfer</code></dt> + <dd>{{domxref("DataTransfer")}} 对象包含了要写入剪贴板的数据。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>当数据被写入到剪贴板的时候,{{jsxref("Promise")}} resolve 回调被执行。如果剪贴板不能完成剪贴操作,{{jsxref("Promise")}} reject 回调被执行。</p> + +<h2 id="示例">示例</h2> + +<p>这个例子展示了如何将当前剪贴板的内容替换为给定的内容。</p> + +<pre class="brush: js">function setClipboard(text) { + let data = new DataTransfer(); + + data.items.add("text/plain", text); + navigator.clipboard.write(data).then(function() { + /* success */ + }, function() { + /* failure */ + }); +} +</pre> + +<p>代码创建了一个 {{domxref("DataTransfer")}} 对象,要替换的内容存储在这里。执行 {{domxref("DataTransferItemList.add()")}} 将数据写入进去,然后执行 <code>write()</code> 方法,指定执行成功或错误的结果。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('Clipboard API','#h-clipboard-write-data','write()')}}</td> + <td>{{Spec2('Clipboard API')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Clipboard.write")}}</p> |