HTML5拖放getData()仅适用于Chrome中的删除事件? [英] HTML5 Drag and Drop getData() only works on drop event in Chrome?

查看:147
本文介绍了HTML5拖放getData()仅适用于Chrome中的删除事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML5拖放API在工作中提供额外的功能,包括将项目拖入和移出浏览器。我目前正在遇到Chrome特有的问题(否则只能在Firefox中进行测试,可以预期)。

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. I am currently coming across issues which are specific to Chrome (otherwise only tested in Firefox, which works as expected).

问题是我无法使用 event.dataTransfer.getData(type)方法返回 dragstart 事件中的数据集,除了 drop 事件。

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.

我设置了这样的事件,绑定到 dragstart 事件(它的消防):

I set the event like so, after binding to the dragstart event (which DOES fire):

event.dataTransfer.setData('text/plain', "some string")

然后在 drop 事件中,我可以获取数据。

Then in the drop event, I can get the data fine.

event.dataTransfer.getData('text/plain')

然而,我不能使用与上述相同的方法在任何其他事件(例如 dragover )。即使我在调用 setData()(即在 dragstart 回调之后)尝试使用上述方法),那么它仍然会返回 undefined

However I cannot use the same method as above on any other events (such as dragover). Even if I try and use the above method on the line after calling setData() (i.e., in the dragstart callback), then it will still return undefined.

所以,在Chrome中,问题是 getData 在Chrome中将始终返回 undefined ,除了 drop 事件回调之外。 (在Firefox中,我可以成功获取正确的数据。)

So, in Chrome, the issue is that getData in Chrome will always return undefined, except within the drop event callback. (In Firefox, I can successfully get the correct data.)

如果您有对 dataTransfer 对象的引用相同的拖动元素,那么为什么你不能在数据丢失之前获取数据?

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?

只是想知道:


  • 以前是否有人遇到过Chrome的问题?

  • 有哪些解决方法?

  • 或者Chrome是否需要修复?

资源:
HTML5拖放规范

推荐答案

WebKit,因此,Chrome在限制您何时可以调用 getData 。您不能在 dragstart dragover 。我认为这是规范错误

WebKit, and hence Chrome, is quite restrictive on when you can call getData. You're not allowed to do it inside dragstart or dragover. I think this is the canonical bug.

这篇关于HTML5拖放getData()仅适用于Chrome中的删除事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆