简体   繁体   English

HTML5拖放getData()仅适用于Chrome中的drop事件?

[英]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: 就是想:

  • Has anyone had this trouble with Chrome before? 以前有没有人遇到Chrome的这个问题?
  • What workarounds are there? 有什么变通方法?
  • Or, is it something Chrome needs to fix? 或者,Chrome需要解决的问题是什么?

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 . 你不允许在dragstartdragover 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.

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