DataTransferItem:kind 属性
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年11月.
DataTransferItem.kind 只读属性返回 DataTransferItem 对象所代表的拖拽数据项的类型(string 或 file)。
值
表示拖拽数据项的类型的字符串。它必须是以下值之一:
示例
下述示例展示了 kind 属性的用法。
js
function dropHandler(ev) {
console.log("放置");
ev.preventDefault();
for (const item of ev.dataTransfer.items) {
if (item.kind === "string" && item.type.match("^text/plain")) {
// 该项是目标节点
item.getAsString((s) => {
ev.target.appendChild(document.getElementById(s));
});
} else if (item.kind === "string" && item.type.match("^text/html")) {
// 拖拽数据项是 HTML
console.log("……放置:HTML");
} else if (item.kind === "file" && item.type.match("^image/")) {
// 拖拽数据项是图片文件
const f = item.getAsFile();
console.log("……放置:文件");
}
}
}
规范
| Specification |
|---|
| HTML> # dom-datatransferitem-kind-dev> |