aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/inputevent/datatransfer/index.html
blob: 680c567b7fcd26c89c9a9cadc85ecb931127ab06 (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
67
68
69
70
---
title: InputEvent.dataTransfer
slug: Web/API/InputEvent/dataTransfer
tags:
  - API
  - DOM Events
  - DataTransfer
  - InputEvent
translation_of: Web/API/InputEvent/dataTransfer
---
<p>{{SeeCompatTable}}{{APIRef("DOM Events")}}</p>

<p>{{domxref("InputEvent")}} 接口中的只读属性 <strong><code>dataTransfer</code></strong> 返回一个 {{domxref("DataTransfer")}} 对象,该对象包含有关要添加到可编辑内容,或从可编辑内容中删除的富文本或纯文本数据的信息。</p>

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

<pre class="syntaxbox">var <em>dataTransfer</em> = <em>inputEvent</em>.dataTransfer</pre>

<h3 id="返回值">返回值</h3>

<p>一个 {{domxref("DataTransfer")}} 对象。</p>

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

<p>在下面的简单示例中,我们在 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event">input</a> 事件上设置了一个事件监听器,以便在将任何内容粘贴到 {{htmlelement("p")}} 元素时,通过 <code><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/DataTransfer/getData">InputEvent.dataTransfer.getData()</a></code> 方法检索其HTML源代码,并在输入框下面的段落中报告。</p>

<p>尝试复制并粘贴提供的部分内容以查看效果。注意,部分浏览器对其支持不佳。</p>

<pre class="brush: html">&lt;p&gt;&lt;span style="font-weight: bold; color: blue"&gt;Whoa, bold blue text!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight: italic; color: red"&gt;Exciting: italic red text!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Boring normal text ;-(&lt;/p&gt;

&lt;hr&gt;

&lt;p contenteditable="true"&gt;Go on, try pasting some content into this editable paragraph and see what happens!&lt;/p&gt;

&lt;p class="result"&gt;&lt;/p&gt;</pre>

<pre class="brush: js">var editable = document.querySelector('p[contenteditable]');
var result = document.querySelector('.result')
var dataTransferObj;

editable.addEventListener('input', (e) =&gt; {
  result.textContent = e.dataTransfer.getData('text/html');
});</pre>

<p>{{EmbedLiveSample('Examples', '100%', 250)}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('InputEvents2','#dom-inputevent-datatransfer','dataTransfer')}}</td>
   <td>{{Spec2('InputEvents2')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

<div>
<p>{{Compat("api.InputEvent.dataTransfer")}}</p>
</div>