HTML 5 drag and drop How to figure out whether an element accepts a drop

假如想象 提交于 2019-12-13 03:44:05

问题


HTML5 drag and drop, trying to work out whether to allow a drop on an element.

So according to this :

http://dev.w3.org/html5/spec-preview/dnd.html#dndevents

You cannot read the DataTransfer dataStore on dragover. Since dragover is the event you are supposed to cancel to indicate that you accept a drop - and given that during dragOver you cannot tell what is being dragged, how is this supposed to work ? I am missing something obvious here ?

It seems to be that you must cancel dragover/dragenter if there is any chance what is being dragged could be dropped on your element, but you can only find out what was getting dragged on the drop.


回答1:


So - we figured out a way to do this.

You can read the dataTransfer.types property which returns a list of the types added via setData. So if you use a custom string as one of those types, you can check that to verify the existence of drag data that you added.

e.g.

event.dataTransfer.setData("SpecialNameOfDragSource", data);

Then in dragover you can see if this type exists :

function ondragover(ev) {
    if (ev.dataTransfer.types && ev.dataTransfer.types.indexOf("SpecialNameOfDragSource") > -1) {
         // cancel the event
    }
 }


来源:https://stackoverflow.com/questions/17234307/html-5-drag-and-drop-how-to-figure-out-whether-an-element-accepts-a-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!