DataTransfer: clearData() 方法
DataTransfer.clearData()
方法删除给定类型的拖动操作的拖动数据。如果给定类型的拖动数据不存在,则此方法不执行任何操作。
如果此方法在没有参数的情况下被调用,或者格式为空字符串,则所有类型的拖动数据将被删除。
此方法不会删除拖动操作中的文件,因此如果拖动操作中包含任何文件,则对象中的 DataTransfer.types
列表中仍然可能存在类型为 "Files"
的条目。
注意:此方法只能在 dragstart
事件的处理程序中使用,因为这是拖动操作的数据存储唯一可写的时间。
语法
js
clearData()
clearData(format)
参数
format
可选-
指定要删除的数据类型的字符串。如果此参数为空字符串或未提供,则删除所有类型的数据。
返回值
无 (undefined
)。
示例
此示例演示了如何使用 DataTransfer
对象的 getData()
、setData()
和 clearData()
方法。
HTML
html
<span class="tweaked" id="source" draggable="true">
Select this element, drag it to the Drop Zone and then release the selection
to move the element.
</span>
<span class="tweaked" id="target">Drop Zone</span>
<div>Status: <span id="status">Drag to start</span></div>
<div>Data is: <span id="data">uninitialized</span></div>
CSS
css
span.tweaked {
display: inline-block;
margin: 1em 0;
padding: 1em 2em;
}
#source {
color: blue;
border: 1px solid black;
}
#target {
border: 1px solid black;
}
JavaScript
js
window.addEventListener("DOMContentLoaded", () => {
// Select HTML elements
const draggable = document.getElementById("source");
const droppable = document.getElementById("target");
const status = document.getElementById("status");
const data = document.getElementById("data");
let dropped = false;
// Register event handlers
draggable.addEventListener("dragstart", dragStartHandler);
draggable.addEventListener("dragend", dragEndHandler);
droppable.addEventListener("dragover", dragOverHandler);
droppable.addEventListener("dragleave", dragLeaveHandler);
droppable.addEventListener("drop", dropHandler);
function dragStartHandler(event) {
status.textContent = "Drag in process";
// Change target element's border to signify drag has started
event.currentTarget.style.border = "1px dashed blue";
// Start by clearing existing clipboards; this will affect all types since we
// don't give a specific type.
event.dataTransfer.clearData();
// Set the drag's format and data (use event target's id for data)
event.dataTransfer.setData("text/plain", event.target.id);
data.textContent = event.dataTransfer.getData("text/plain");
}
function dragEndHandler(event) {
if (!dropped) {
status.textContent = "Drag canceled";
}
data.textContent = event.dataTransfer.getData("text/plain") || "empty";
// Change border to signify drag is no longer in process
event.currentTarget.style.border = "1px solid black";
if (dropped) {
// Remove all event listeners
draggable.removeEventListener("dragstart", dragStartHandler);
draggable.removeEventListener("dragend", dragEndHandler);
droppable.removeEventListener("dragover", dragOverHandler);
droppable.removeEventListener("dragleave", dragLeaveHandler);
droppable.removeEventListener("drop", dropHandler);
}
}
function dragOverHandler(event) {
status.textContent = "Drop available";
event.preventDefault();
}
function dragLeaveHandler(event) {
status.textContent = "Drag in process (drop was available)";
event.preventDefault();
}
function dropHandler(event) {
dropped = true;
status.textContent = "Drop done";
event.preventDefault();
// Get data linked to event format « text »
const _data = event.dataTransfer.getData("text/plain");
const element = document.getElementById(_data);
// Append drag source element to event's target element
event.target.appendChild(element);
// Change CSS styles and displayed text
element.style.cssText =
"border: 1px solid black;display: block; color: red";
element.textContent = "I'm in the Drop Zone!";
}
});
规范
规范 |
---|
HTML 标准 # dom-datatransfer-cleardata-dev |
浏览器兼容性
BCD 表格仅在启用 JavaScript 的浏览器中加载。