javascript - preventDefault() 在拖放中阻止了什么?

标签 javascript html

例如,这里是来自 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));
}

最佳答案

You must cancel the default action for ondragenter and ondragover in order for ondrop to fire. In the case of a div, the default action is not to drop. This can be contrasted with the case of an input type=text element, where the default action is to drop. In order to allow a drag-and-drop action on a div, you must cancel the default action

关于javascript - preventDefault() 在拖放中阻止了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775112/

相关文章:

javascript - jQuery - 如何使输入值 ("email"、 "password")在失焦时重新出现?

c# - SignalR Hub 未执行指定的 javascript

PHP mail() 无法运行

Javascript 拖动事件不起作用

jquery - Anything Slider - 使用不同尺寸的图像和字幕

javascript - react-native - 图片需要来自 JSON 的本地路径

javascript - 为什么将联系人添加到数组的方法不起作用?

html - 从 Angular 模板获取字符串而不是预期数字并输入 native 元素的值

HTML/CSS : Vertical aligning span with vertical-align and line-height

javascript - 加载网站时隐藏所有<table>