[英]HTML5 Drag and Drop getData() only works on drop event in Chrome?
I'm currently working on a project at work using the HTML5 Drag and Drop API to provide added functionality including dragging items in and out of browsers. 我目前正在开发一个使用HTML5拖放API的项目来提供附加功能,包括将项目拖入和拖出浏览器。 I am currently coming across issues which are specific to Chrome (otherwise only tested in Firefox, which works as expected).
我目前遇到特定于Chrome的问题(否则只在Firefox中测试,它按预期工作)。
The issue is that I cannot use the event.dataTransfer.getData(type)
method to return the data set on the dragstart
event in any events except the drop
event. 问题是我不能使用
event.dataTransfer.getData(type)
方法在除drop
事件之外的任何事件中返回dragstart
事件上的数据集。
I set the event like so, after binding to the dragstart
event (which DOES fire): 在绑定到
dragstart
事件(发生火灾)之后,我设置了这样的事件:
event.dataTransfer.setData('text/plain', "some string")
Then in the drop
event, I can get the data fine. 然后在
drop
事件中,我可以获得数据。
event.dataTransfer.getData('text/plain')
However I cannot use the same method as above on any other events (such as dragover
). 但是我不能在任何其他事件(例如
dragover
)上使用与上面相同的方法。 Even if I try and use the above method on the line after calling setData()
(ie, in the dragstart
callback), then it will still return undefined
. 即使我在调用
setData()
之后尝试在线上使用上面的方法(即在dragstart
回调中),它仍将返回undefined
。
So, in Chrome, the issue is that getData
in Chrome will always return undefined
, except within the drop
event callback. 因此,在Chrome中,问题是Chrome中的
getData
将始终返回undefined
,但在drop
事件回调中除外。 (In Firefox, I can successfully get the correct data.) (在Firefox中,我可以成功获取正确的数据。)
If you have the reference to the dataTransfer
object of the same dragging element, then why should you not be able to get the data until it is dropped? 如果您对同一拖动元素的
dataTransfer
对象有引用,那么为什么在删除数据之前无法获取数据?
Just wondering: 就是想:
Resources: Specification for HTML5 drag and drop. 资源: HTML5拖放规范。
WebKit, and hence Chrome, is quite restrictive on when you can call getData
. 当你可以调用
getData
时,WebKit,以及Chrome,是非常严格的限制。 You're not allowed to do it inside dragstart
or dragover
. 你不允许在
dragstart
或dragover
。 I think this is the canonical bug . 我认为这是规范的错误 。
Referencing this answer : 参考这个答案 :
The data is only available on drop, this is a security feature since a website could grab data when you happen to be dragging something across the webpage.
这些数据仅在drop下可用,这是一项安全功能,因为当您在网页上拖动某些内容时,网站可能会抓取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.