只读属性DataTransferItem.type
返回代表拖动数据项的 DataTransferItem
对象的类型(格式)。 type
是一个Unicode字符串,通常由MIME给出,不过不需要MIME类型。
举例一些类型: text/plain
和 text/html
.
语法
dataItem.type;
返回值
一个代表拖动数据项类型的 DOMString
。
示例
这个例子演示了type
属性的用法。
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 Living Standard type |
Living Standard | Initial version |
HTML 5.1 type |
Recommendation | Snapshot of the HTML WHATWG document |
浏览器兼容性
BCD tables only load in the browser
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.