DataTransferItem.getAsString() method invokes the given callback with the drag data item's string data as the argument if the item's kind Plain unicode string (即 kind is string ).

句法

dataTransferItem.getAsString(callback);
					

参数

callback
A callback function that has access to the data transfer item's string data. See Callback below for details.

返回值

undefined

Callback

The callback parameter is a callback function which accepts one parameter:

DOMString

The drag data item's string data.

The callback return value is undefined .

范例

This example shows the use of the getAsString() method as an inline function drop event handler.

function drop_handler(ev) {
 console.log("Drop");
 ev.preventDefault();
 var data = ev.dataTransfer.items;
 for (var i = 0; i < data.length; i += 1) {
   if ((data[i].kind == 'string') &&
       (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') &&
              (data[i].type.match('^text/html'))) {
     // Drag data item is HTML
     console.log("... Drop: HTML");
   } else if ((data[i].kind == 'string') &&
              (data[i].type.match('^text/uri-list'))) {
     // Drag data item is URI
     console.log("... Drop: URI");
   } else if ((data[i].kind == 'file') &&
              (data[i].type.match('^image/'))) {
     // Drag data item is an image file
     var f = data[i].getAsFile();
     console.log("... Drop: File ");
   }
 }
}
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'getAsString()' in that specification.
实时标准 初始定义。
HTML 5.1
The definition of 'getAsString()' in that specification.
推荐 Snapshot fo HTML WHATWG document

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getAsString Chrome 11 Edge 12 Firefox 50 IE No Opera 12 Safari 5.1 WebView Android 4 Chrome Android 18 Firefox Android 50 Opera Android No Safari iOS 5 Samsung Internet Android 1.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: