aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/clipboard/writetext/index.html
blob: 2b6d77f43d006dfee89d14d91a3a817026eb2fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
title: Clipboard.writeText()
slug: Web/API/Clipboard/writeText
tags:
  - API
  - Clip
  - Clipboard
  - Clipboard API
  - writeText
translation_of: Web/API/Clipboard/writeText
---
<div>{{APIRef("Clipboard API")}}</div>

<p>{{domxref("Clipboard")}} 接口的 <strong><code>writeText()</code></strong> 方法可以写入特定字符串到操作系统的剪切板。</p>

<div class="note">
<p><strong>Note:</strong> 规范要求在写入剪贴板之前使用 <a href="/en-US/docs/Web/API/Permissions_API">Permissions API</a> 获取“剪贴板写入”权限。但是,不同浏览器的具体要求不同,因为这是一个新的API。有关详细信息,请查看<a href="#浏览器兼容性">浏览器兼容性</a> and {{SectionOnPage("/en-US/docs/Web/API/Clipboard", "Clipboard availability")}}</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var <em>promise</em> = navigator.clipboard.writeText(<em>newClipText</em>)</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>newClipText</code></dt>
 <dd>The {{domxref("DOMString")}} to be written to the clipboard.</dd>
 <dt>
 <h3 id="返回值">返回值</h3>
 </dt>
</dl>

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

<h2 id="例子">例子</h2>

<p>此示例将剪贴板的内容设置为字符串“&lt;empty clipboard&gt;”。</p>

<pre class="brush: js">navigator.clipboard.writeText("&lt;empty clipboard&gt;").then(function() {
  /* clipboard successfully set */
}, function() {
  /* clipboard write failed */
});
</pre>

<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-writetext-data','writeText()')}}</td>
   <td>{{Spec2('Clipboard API')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.Clipboard.writeText")}}</p>