HTML Drag and Drop API
如果拖拽数据项的 DataTransferItem.kind 是纯 Unicode 字符串(即 kind 为 string),DataTransferItem.getAsString() 方法会调用给定的回调函数,并将该拖拽数据项的字符串数据作为参数传入。
语法
getAsString(callbackFn)
参数
callbackFn- : 一个回调函数,接收以下参数:
data- : DataTransferItem 的字符串数据。
- : 一个回调函数,接收以下参数:
返回值
无(undefined)。
示例
本示例展示了在 HTMLElement/drop_event 事件处理器中将 getAsString() 方法用作内联函数的用法。
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 === "string" && item.type.match("^text/uri-list")) {
// 拖拽数据项是 URI
console.log("… 放置:URI");
} else if (item.kind === "file" && item.type.match("^image/")) {
// 拖拽数据项是图像文件
const f = item.getAsFile();
console.log("… 放置:文件");
}
}
}
规范
浏览器兼容性
参见
- DataTransfer.getData()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
