HTML Drag and Drop API
DataTransferItem.kind 只读属性返回 DataTransferItem 对象所代表的拖拽数据项的类型(string 或 file)。
值
表示拖拽数据项的类型的字符串。它必须是以下值之一:
'file'- : 如果拖拽数据项是文件。
'string'- : 如果拖拽数据项的类型是纯 Unicode 字符串。
示例
下述示例展示了 kind 属性的用法。
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("……放置:文件");
}
}
}
规范
浏览器兼容性
参见
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
