简体   繁体   English

HTML5拖放ondragover未在Chrome中触发

[英]HTML5 Drag and Drop ondragover not firing in 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: 我在这里有一个简单的例子,它不是在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);

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. 它似乎在safari中工作得很好......但是在Chrome中,当红色方块触摸虚线时,不会调用dragover事件。

I've tried to replicate some examples which have this currently working in chrome but it din't work for me either. 我试图复制一些目前正在使用chrome的示例,但它对我来说也不起作用。

I've tried prevent the default behaviour to see it if worked but it didn't. 我已经尝试阻止默认行为,如果工作但看不到它。 any help would be very appreciated. 任何帮助将非常感激。

thanks 谢谢

It seems that it is needed to set some data to the draggable element on the dragstart event for the dragover event to be fired on the dropzone. 似乎需要将一些数据设置为dragstart事件中的draggable元素,以便在dropzone上触发dragover事件。

I've updated the snippet http://jsfiddle.net/G9mJw/20/ which now works in chrome as well. 我已经更新了片段http://jsfiddle.net/G9mJw/20/ ,它现在也适用于chrome。

and the new code as follows: 和新代码如下:

var dropzone = document.getElementById('dropzone'),
    draggable = document.getElementById('draggable');


function onDragStart(event) {
    event.dataTransfer.setData('text/html', null); //cannot be empty string
}

function onDragOver(event) {
    var counter = document.getElementById('counter');
    counter.innerText = parseInt(counter.innerText, 10) + 1;
}   

draggable.addEventListener('dragstart', onDragStart, false);
dropzone.addEventListener('dragover', onDragOver, false);

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: 还有一些关于它如何工作的更多信息: https//developer.mozilla.org/En/DragDrop/Drag_Operations#Drag_Data ,这提到的事情如下:

When a drag occurs, data must be associated with the drag which identifies what is being dragged. 发生拖动时,数据必须与拖动相关联,该拖动标识正在拖动的内容。

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? 哪个更容易理解......我只是想弄清楚它在Safari中是如何工作的,而不需要发送一些数据? or perhaps it already send some content as default? 或者它可能已经发送了一些默认内容?

Also the event.dataTransfer.setData('text/html', null); 还有event.dataTransfer.setData('text/html', null); method, curiously cannot send an empty string like event.dataTransfer.setData('text/html', ''); 方法,奇怪的是不能发送像event.dataTransfer.setData('text/html', '');这样的空字符串event.dataTransfer.setData('text/html', ''); otherwise the dragover event will not be dispatched. 否则将不会发送dragover事件。

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. Chrome目前仅支持一些数据类型 - 如果您的数据没有可识别的MIME类型,则拖放操作就不会继续。 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). 这是很清楚违反了W3C规范,而不是在Firefox的问题(只要你提供某种形式的数据),甚至Safari浏览器(它允许拖动进行数据是否设置与否)。

The Chromium bug report is here: http://code.google.com/p/chromium/issues/detail?id=93514 Chromium错误报告位于: http//code.google.com/p/chromium/issues/detail?id = 93514

I had trouble with mime types. 我遇到了mime类型的问题。

W3Schools has a page you can experiment with: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop2 W3Schools有一个你可以试验的页面: 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". 在我将getData和setData更改为“text / html”而不是“Text”之前,他们的代码示例对我不起作用。

I am using: Version 34.0.1847.116 Ubuntu 14.04 aura (260972) 我正在使用:版本34.0.1847.116 Ubuntu 14.04光环(260972)

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

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