简体   繁体   English

如何从事件Dragover或Dragenter中的DataTransfer.getData获取数据

[英]how to get data from DataTransfer.getData in event Dragover or Dragenter

i have a code : 我有一个代码:

element.addEventListener('dragstart',function(e){
   e.dataTransfer.setData('Text', 'something is here');
},false);

when i getdata from DataTransfer.getData in event Drop is running. 当我在事件Drop运行时从DataTransfer.getData获取数据时。 but when i want get data in event dragover or dragenter, data is null. 但是当我想在事件dragover或dragenter中获取数据时,数据为空。

element.addEventListener('dragover',function(e){
   var a = e.dataTransfer.getData('Text');
   console.log(a);
},false);

So, how to get data in event dragover or dragenter 那么,如何在事件dragover或dragenter中获取数据

Normally you don't have access to this information on events other than dragstart and drop . 通常,您无法访问除dragstartdrop之外的事件的此信息。 Firefox seems to give you access but it seems to go against the standard. Firefox似乎为您提供访问权限,但它似乎违反了标准。

The way the data is transfered during a drag and drop is through a data store object, that contains all the information needed for the different operations to happen. 在拖放过程中传输数据的方式是通过data store对象,该对象包含发生不同操作所需的所有信息。 But there are certain restrictions to what you can do with this information depending on the event on which you access this data store. 但是,根据您访问此数据存储的event ,您可以对此信息执行某些限制。 There are 3 modes, that are defined as follow: 有3种模式,定义如下:

A drag data store mode, which is one of the following: 拖动数据存储模式,它是以下之一:

Read/write mode 读/写模式

For the dragstart event. 对于dragstart事件。 New data can be added to the drag data store. 可以将新数据添加到拖动数据存储中。

Read-only mode 只读模式

For the drop event. 对于掉落事件。 The list of items representing dragged data can be read, including the data. 可以读取表示拖动数据的项目列表,包括数据。 No new data can be added. 无法添加新数据。

Protected mode 保护模式

For all other events. 对于所有其他活动。 The formats and kinds in the drag data store list of items representing dragged data can be enumerated, but the data itself is unavailable and no new data can be added. 可以枚举拖动数据存储表示拖动数据的项目列表中的格式和种类,但数据本身不可用,并且不能添加新数据。

https://html.spec.whatwg.org/multipage/interaction.html#the-drag-data-store https://html.spec.whatwg.org/multipage/interaction.html#the-drag-data-store

So on dragover, the data store is in protected mode, hence the data shouldn't be available. 因此,在dragover上,数据存储处于保护模式,因此数据不可用。 Again, Firefox implements this differently, but you shouldn't rely on it in any case. 同样,Firefox以不同的方式实现了这一点,但在任何情况下都不应该依赖它。

These modes are there for security reasons, these data transfer allows transfer not only of elements of a same page, but of data from other applications, files, etc. 出于安全原因,这些模式存在,这些数据传输不仅允许传输同一页面的元素,而且传输来自其他应用程序,文件等的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM