aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/datatransferitem/getasstring/index.html
blob: ce731b39ddd00ad6a70c5f465cba1b997d33b4ca (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
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
---
title: DataTransferItem.getAsString()
slug: Web/API/DataTransferItem/getAsString
translation_of: Web/API/DataTransferItem/getAsString
---
<div>{{APIRef("HTML Drag and Drop API")}}</div>

<p> <strong><code>DataTransferItem.getAsString()</code></strong>  当DataTransferItem对象的kind属性是一个普通Unicode字符串时,该方法会用 DataTransferItem对象的kind属性作为入参来执行传入的回调函数 (i.e. <code>kind</code> is <code>string</code>).</p>

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

<pre class="syntaxbox notranslate"><em>dataTransferItem</em>.getAsString(callback);
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>callback</code></dt>
 <dd>A callback function that has access to the {{domxref("DataTransferItem","data transfer item's")}} string data. See <a href="#callback">Callback</a> below for details.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>{{jsxref("undefined")}}</p>

<h2 id="Callback">Callback</h2>

<p>The callback parameter is a callback function which accepts one parameter:</p>

<dl>
 <dt>{{domxref("DOMString")}}</dt>
 <dd>The drag data item's string data.</dd>
</dl>

<p>The callback return value is <code>undefined</code>.</p>

<h2 id="Example">Example</h2>

<p>This example shows the use of the <code>getAsString()</code> method as an <em>inline function</em> in a {{event("drop")}} event handler.</p>

<pre class="brush: js notranslate">function drop_handler(ev) {
 console.log("Drop");
 ev.preventDefault();
 var data = ev.dataTransfer.items;
 for (var i = 0; i &lt; data.length; i += 1) {
   if ((data[i].kind == 'string') &amp;&amp;
       (data[i].type.match('^text/plain'))) {
     // This item is the target node
     data[i].getAsString(function (s){
       ev.target.appendChild(document.getElementById(s));
     });
   } else if ((data[i].kind == 'string') &amp;&amp;
              (data[i].type.match('^text/html'))) {
     // Drag data item is HTML
     console.log("... Drop: HTML");
   } else if ((data[i].kind == 'string') &amp;&amp;
              (data[i].type.match('^text/uri-list'))) {
     // Drag data item is URI
     console.log("... Drop: URI");
   } else if ((data[i].kind == 'file') &amp;&amp;
              (data[i].type.match('^image/'))) {
     // Drag data item is an image file
     var f = data[i].getAsFile();
     console.log("... Drop: File ");
   }
 }
}
</pre>

<h2 id="Specifications">Specifications</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('HTML WHATWG', 'interaction.html#dom-datatransferitem-getasstring','getAsString()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', 'editing.html#dom-datatransferitem-getasstring','getAsString()')}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>Snapshot fo HTML WHATWG document</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.DataTransferItem.getAsString")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("DataTransfer.getData()")}}</li>
</ul>