HTML5 拖放 ondragover 在 Chrome 中未触发
我这里有一个简单的例子,它在 Chrome 11 中对我来说没有触发 http://jsfiddle.net/G9mJw/ 它由一个非常简单的代码组成:
var dropzone = document.getElementById('dropzone'),
draggable = document.getElementById('draggable');
function onDragOver(event) {
var counter = document.getElementById('counter');
counter.innerText = parseInt(counter.innerText, 10) + 1;
}
dropzone.addEventListener('dragover', onDragOver, false);
它似乎在 safari 中工作正常...但是在 Chrome 中,当红色方块触摸虚线方块时,不会调用 dragover
事件。
我尝试复制一些当前在 chrome 中工作的示例,但它对我也不起作用。
我尝试阻止默认行为以查看它是否有效,但没有成功。任何帮助将不胜感激。
谢谢
I have this simple example here which is not firing in Chrome 11 for me http://jsfiddle.net/G9mJw/ which consists on a very simple code:
var dropzone = document.getElementById('dropzone'),
draggable = document.getElementById('draggable');
function onDragOver(event) {
var counter = document.getElementById('counter');
counter.innerText = parseInt(counter.innerText, 10) + 1;
}
dropzone.addEventListener('dragover', onDragOver, false);
It seems to work fine in safari tho...but in Chrome the dragover
event is not being called when the red square touch the dotted one.
I've tried to replicate some examples which have this currently working in chrome but it din't work for me either.
I've tried prevent the default behaviour to see it if worked but it didn't. any help would be very appreciated.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎需要在
dragstart
事件上为可拖动元素设置一些数据,以便在 dropzone 上触发dragover
事件。我已经更新了代码片段 http://jsfiddle.net/G9mJw/20/ 现在可以在铬也是如此。
新代码如下:
此外还有一些有关其工作原理的更多信息:
https://developer.mozilla.org/En/DragDrop/Drag_Operations#Drag_Data这提到了这样的事情:
这使得更容易理解...
我只是想弄清楚这在 Safari 中如何工作而不需要发送一些数据?或者它可能已经默认发送一些内容?
还有
event.dataTransfer.setData('text/html', null);
方法,奇怪的是不能发送像event.dataTransfer.setData('text/html', '' );
否则,dragover
事件将不会被调度。It seems that it is needed to set some data to the draggable element on the
dragstart
event for thedragover
event to be fired on the dropzone.I've updated the snippet http://jsfiddle.net/G9mJw/20/ which now works in chrome as well.
and the new code as follows:
Also there's some more information about how this works at:
https://developer.mozilla.org/En/DragDrop/Drag_Operations#Drag_Data and this mention things like:
Which make easier to understand...
I am just trying to figure out how does this works in Safari without the need to send some data? or perhaps it already send some content as default?
Also the
event.dataTransfer.setData('text/html', null);
method, curiously cannot send an empty string likeevent.dataTransfer.setData('text/html', '');
otherwise thedragover
event will not be dispatched.Chrome 目前仅支持几种数据类型 - 如果您的数据没有可识别的 MIME 类型,则拖放操作将无法继续。这显然违反了 W3C 规范,并且在 Firefox(只要您提供某种类型的数据)甚至 Safari(无论数据是否设置都允许拖动继续进行)中都不是问题或不)。
Chromium 错误报告位于:http://code.google.com/ p/chromium/issues/detail?id=93514
Chrome currently only supports a few data types — if your data does not have a recognized MIME Type the drag/drop simply doesn't proceed. This is very clearly in violation of the W3C Spec, and is not a problem in Firefox (so long as you provide some sort of data) or even Safari (which allows the drag to proceed whether data is set or not).
The Chromium bug report is here: http://code.google.com/p/chromium/issues/detail?id=93514
我在哑剧类型方面遇到了麻烦。
W3Schools 有一个页面您可以尝试:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop2
他们的代码示例对我不起作用,直到我将 getData 和 setData 更改为“text/html”而不是“Text”。
我正在使用:
版本 34.0.1847.116 Ubuntu 14.04 光环 (260972)
I had trouble with mime types.
W3Schools has a page you can experiment with:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop2
Their code sample would not work for me until I changed getData and setData to "text/html" instead of "Text".
I am using:
Version 34.0.1847.116 Ubuntu 14.04 aura (260972)