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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
---
title: 'Element: paste事件'
slug: Web/Events/paste
tags:
- Clipboard API
- Event
- Reference
translation_of: Web/API/Element/paste_event
---
<p> {{APIRef}}</p>
<p>当用户在浏览器用户界面发起“粘贴”操作时,会触发<strong><code>paste</code></strong>事件。</p>
<pre class="syntaxbox"><strong>冒泡</strong> 是
<strong>可取消</strong> 是
<strong>接口</strong> {{domxref("ClipboardEvent")}}
<strong>事件处理属性</strong> {{domxref("HTMLElement/onpaste", "onpaste")}}
</pre>
<p><span style="font-size: 1rem; letter-spacing: -0.00278rem;">如果光标位于可编辑的上下文中(例如,在 {{HTMLElement("textarea")}} 或者 </span><code style="font-style: normal; font-size: 1rem; letter-spacing: -0.00278rem;"><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable">contenteditable</a></code><span style="font-size: 1rem; letter-spacing: -0.00278rem;"> 属性设置为 </span><code style="font-style: normal; font-size: 1rem; letter-spacing: -0.00278rem;">true的元素</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">),则默认操作是将剪贴板的内容插入光标所在位置的文档中。</span></p>
<p>事件处理程序可以通过调用事件的 <code>clipboardData</code> 属性上的 {{domxref("DataTransfer/getData", "getData()")}}访问剪贴板内容。</p>
<p>要覆盖默认行为(例如,插入一些不同的数据或转换剪贴板的内容),事件处理程序必须使用 {{domxref("Event/preventDefault", "event.preventDefault()")}},取消默认操作,然后手动插入想要的数据。</p>
<p>可以构造和分派<a href="https://developer.mozilla.org/zh-CN/docs/Web/Guide/Events/Creating_and_triggering_events">合成的</a><code>paste</code>事件,但这不会影响文档内容。</p>
<h2 id="举例">举例</h2>
<h3 id="Live_example">Live example</h3>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><div class="source" contenteditable="true">Try copying text from this box...</div>
<div class="target" contenteditable="true">...and pasting it into this one</div></pre>
<h4 id="CSS">CSS</h4>
<pre class="brush: css">div.source, div.target {
border: 1px solid gray;
margin: .5rem;
padding: .5rem;
height: 1rem;
background-color: #e9eef1;
}
</pre>
<h4 id="JS">JS</h4>
<pre class="brush: js">const target = document.querySelector('div.target');
target.addEventListener('paste', (event) => {
let paste = (event.clipboardData || window.clipboardData).getData('text');
paste = paste.toUpperCase();
const selection = window.getSelection();
if (!selection.rangeCount) return false;
selection.deleteFromDocument();
selection.getRangeAt(0).insertNode(document.createTextNode(paste));
event.preventDefault();
});
</pre>
<h4 id="Result">Result</h4>
<p> {{EmbedLiveSample('Live_example', '100%', '100px')}}</p>
<h2 id="规范">规范</h2>
<table>
<thead>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('Clipboard API', '#clipboard-event-paste')}}</td>
<td>{{Spec2('Clipboard API')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>
<p>{{Compat("api.Element.paste_event")}}</p>
<h2 id="另见">另见</h2>
<ul>
<li>Related events: {{domxref("Element/cut_event", "cut")}}, {{domxref("Element/copy_event", "copy")}}</li>
<li>This event on {{domxref("Document")}} targets: {{domxref("Document/paste_event", "paste")}}</li>
<li>This event on {{domxref("Window")}} targets: {{domxref("Window/paste_event", "paste")}}</li>
</ul>
|