DataTransfer.clearData() - Web APIs 编辑

The DataTransfer.clearData() method removes the drag operation's drag data for the given type. If data for the given type does not exist, this method does nothing.

If this method is called with no arguments or the format is an empty string, the data of all types will be removed.

This method does not remove files from the drag operation, so it's possible for there still to be an entry with the type "Files" left in the object's DataTransfer.types list if there are any files included in the drag.

This method can only be used in the handler for the dragstart event, because that's the only time the drag operation's data store is writeable.

Syntax

DataTransfer.clearData([format]);

Parameters

format Optional
A string which specifies the type of data to remove. If this parameter is an empty string or is not provided, the data for all types is removed.

Example

This example shows the use of the DataTransfer object's getData(), setData() and clearData() methods.

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

span.tweaked {
  display: inline-block;
  margin: 1em 0;
  padding: 1em 2em;
}

#source {
  color: blue;
  border: 1px solid black;
}

#target {
  border: 1px solid black;
}

JavaScript

window.addEventListener('DOMContentLoaded', function () {
  // Select HTML elements
  var draggable = document.getElementById('source');
  var dropable = document.getElementById('target');
  var status = document.getElementById('status');
  var data = document.getElementById('data');
  var dropped = false;

  // Register event handlers
  draggable.addEventListener('dragstart', dragStartHandler);
  draggable.addEventListener('dragend', dragEndHandler);
  dropable.addEventListener('dragover', dragOverHandler);
  dropable.addEventListener('dragleave', dragLeaveHandler);
  dropable.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);
      dropable.removeEventListener('dragover', dragOverHandler);
      dropable.removeEventListener('dragleave', dragLeaveHandler);
      dropable.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 »
    var _data = event.dataTransfer.getData('text/plain');
    var 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!";
  }
})

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'DataTransfer.clearData()' in that specification.
Living Standard
HTML 5.1
The definition of 'DataTransfer.clearData()' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:91 次

字数:8076

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文