What prevents default () prevention when dragging and dropping?

For example, here is the drag and drop code from w3schools.com:

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}  

function drop(ev) {
    ev.preventDefault();    
    var data = ev.dataTransfer.getData("text");     
    ev.target.appendChild(document.getElementById(data));
}
+3
source share
2 answers
You must undo the default action for ondragenter and ondragover so that ondrop can fire. In the case of a div, the default action is not canceled. This can be contrasted with the case of entering type = text element, where the default action should be reduced. To enable the drag action on a div, you must undo the default action
0
source
  • Call preventDefault()to prevent the browser from processing the default data (opened by default as a link when deleting)
  • dataTransfer.getData(). , setData()
0

Source: https://habr.com/ru/post/1624173/


All Articles