简体   繁体   English

JavaScript拖曳事件不起作用

[英]Javascript drag over event doesn't work

I'm trying to make a simple drag and drop with native js and it does not seem to work. 我正在尝试使用本机js进行简单的拖放,但这似乎不起作用。 The console.log here doesn't show. 这里没有显示console.log。 What am I missing? 我想念什么?

 var drag = document.getElementById('drag'), drop = document.getElementById('dropped'); drag.addEventListener('dragstart', function(e) { e.preventDefault(); console.log('drag'); }) drop.addEventListener('drop', function(e) { e.preventDefault(); conosle.log(e); }) drop.addEventListener('dragover', function(e) { e.preventDefault(); conosle.log(e); }) 
 #dropped { background: #000; width: 400px; height: 400px; } 
 <div id="drag" draggable="true"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi eveniet illo, nisi placeat neque voluptatem sit ipsam corporis. Veritatis dicta perspiciatis temporibus error amet. Laborum, vero amet magnam nihil debitis!</p> </div> <div id="dropped"> </div> 

You shouldn't prevent default when using native JS for dragging. 使用本机JS拖动时,您不应阻止默认设置。 See here: 看这里:

 var drag = document.getElementById('drag'), drop = document.getElementById('dropped'); drag.addEventListener('dragstart', function(e) { console.log('drag'); }) drop.addEventListener('drop', function(e) { e.preventDefault(); console.log(e); alert('dropped!'); }) drop.addEventListener('dragover', function(e) { e.preventDefault(); }) 
 #dropped { background: #000; width: 400px; height: 400px; } 
 <div id="drag" draggable="true"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi eveniet illo, nisi placeat neque voluptatem sit ipsam corporis. Veritatis dicta perspiciatis temporibus error amet. Laborum, vero amet magnam nihil debitis!</p> </div> <div id="dropped"> </div> 

A little explanation about preventDefault() and where and why it should be used: 关于preventDefault()以及应在何处以及为什么使用它的一些解释:

Calling the preventDefault method during both a dragenter and dragover event will indicate that a drop is allowed at that location. 在Dragenter和Dragover事件期间都调用preventDefault方法将指示在该位置允许放置。 However, you will commonly wish to call the preventDefault method only in certain situations, for example, only if a link is being dragged. 但是,通常希望仅在某些情况下(例如,仅当链接被拖动时)才调用preventDefault方法。 To do this, call a function which checks a condition and only cancels the event when the condition is met. 为此,调用一个检查条件并仅在满足条件时才取消事件的函数。 If the condition is not met, don't cancel the event, and a drop will not occur there if the user releases the mouse button. 如果不满足该条件,请不要取消该事件,并且如果用户释放鼠标按钮,该事件将不会在那里发生。


A listener for the dragenter and dragover events are used to indicate valid drop targets, that is, places where dragged items may be dropped. Dragenter和Dragover事件的侦听器用于指示有效的放置目标,也就是放置放置拖动项的位置。 Most areas of a web page or application are not valid places to drop data. 网页或应用程序的大多数区域都不是放置数据的有效位置。 Thus, the default handling for these events is to not allow a drop. 因此,这些事件的默认处理是不允许丢弃。

If you want to allow a drop, you must prevent the default handling by cancelling the event. 如果要允许丢弃,则必须通过取消事件来防止默认处理。 You can do this either by returning false from an attribute-defined event listener, or by calling the event's event.preventDefault method. 您可以通过从属性定义的事件侦听器返回false或调用事件的event.preventDefault方法来执行此操作。 The latter may be more feasible in a function defined in a separate script. 在单独脚本中定义的函数中,后者可能更为可行。


In a web page, you should call the preventDefault method of the event if you have accepted the drop so that the default browser handling does not handle the dropped data as well. 在网页中,如果您已接受删除,则应调用事件的preventDefault方法,以便默认浏览器处理也无法处理删除的数据。 For example, when a link is dragged to a web page, Firefox will open the link. 例如,当将链接拖到网页上时,Firefox将打开该链接。 By cancelling the event, this behavior will be prevented. 通过取消事件,将防止此行为。


All the quotes taken from: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Drag_operations 所有引号取自: https : //developer.mozilla.org/en-US/docs/Web/Guide/HTML/Drag_operations

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

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