It seems the drop event is not triggering when I would expect.

I assume that the drop event fires when an element that is being dragged is releases above the target element, but this doesn't seem to the the case.

我误解了什么?

http://jsfiddle.net/LntTL/

$('.drop').on('drop dragdrop',function(){
    alert('dropped');
});
$('.drop').on('dragenter',function(){
    $(this).html('drop now').css('background','blue');
})
$('.drop').on('dragleave',function(){
    $(this).html('drop here').css('background','red');
})

推荐答案

为了使drop事件发生在div元素上,必须取消ondragenterondragover事件.使用jquery和您提供的代码...

$('.drop').on('drop dragdrop',function(){
    alert('dropped');
});
$('.drop').on('dragenter',function(event){
    event.preventDefault();
    $(this).html('drop now').css('background','blue');
})
$('.drop').on('dragleave',function(){
    $(this).html('drop here').css('background','red');
})
$('.drop').on('dragover',function(event){
    event.preventDefault();
})

有关更多信息,请查看the MDN page.

Jquery相关问答推荐

通过 Ajax POST 将按钮值从 HTML 发送到 Flask 时出现空数据

第一次单击后,Ajax addEventListener 停止工作

如果选中了三个sibling ,则选中一个复选框

当输入值以编程方式更改时触发更改事件?

如何在 jquery 中从这个日期减go 一周?

对自定义属性执行客户端验证

jQuery从字符串中删除字符串

如何使用 jQuery 重置 jquery Select 的 Select 选项?

JQuery 仅在 Rails 4 应用程序中的页面刷新时加载

如何使用 jQuery 清空输入值?

如何在 HTML选项标签上显示工具提示?

在单击事件上判断 Ctrl / Shift / Alt 键

如何使用jQuery在弹出窗口中预览输入类型=文件中的选定图像?

jQuery ajax (jsonp) 忽略超时并且不触发错误事件

你如何在Javascript中缓存图像

如何从 jQuery UI datepicker 获取日期

在jquery中查找具有某个属性值的所有元素

jquery更改div类的样式属性

如何在调用 Ajax 等异步调用时让代码等待

如何在没有鼠标事件的情况下在 jQuery 中获取鼠标位置?