我正在尝试将文件从桌面拖放到浏览器窗口.我使用 jQuery 将三个事件附加到 HTML 元素,如下面的代码所示:
I'm trying to implement drag and dropping of files from the desktop the browser window. I have used jQuery to attach three events to the HTML element as in the code below:
$("html").on("dragover", function() {
$(this).addClass('dragging');
});
$("html").on("dragleave", function() {
$(this).removeClass('dragging');
});
$("html").on("drop", function(event) {
event.preventDefault();
event.stopPropagation();
alert("Dropped!");
});
'dragover' 和 'dragleave' 事件工作正常,当我将文件拖到上方时会在整个页面周围显示一个嵌入边框,如果我再次将文件拖出则将其删除.
The 'dragover' and 'dragleave' events work fine, displaying an inset border around the entire page when I drag a file over an removing it if I drag the file out again.
但是,'drop' 事件似乎根本没有触发,拖放的文件只是在浏览器窗口中打开.
However, the 'drop' event doesn't seem to fire at all, the dropped file simply opens in the browser window.
有人知道为什么这个事件没有触发吗?
Does anyone have any idea why this event is not firing?
顺便说一句,我正在使用最新版本的 Chrome 并使用 jQuery 1.10.2 进行测试.
Btw, I am testing this in the latest version of Chrome and using jQuery 1.10.2.
你需要取消所有事件
$("html").on("dragover", function(event) {
event.preventDefault();
event.stopPropagation();
$(this).addClass('dragging');
});
$("html").on("dragleave", function(event) {
event.preventDefault();
event.stopPropagation();
$(this).removeClass('dragging');
});
$("html").on("drop", function(event) {
event.preventDefault();
event.stopPropagation();
alert("Dropped!");
});
这篇关于jQuery.on("drop") 没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 javascript 认为文档“准备好"之前,如何让How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 认为文档“准备好
jasmine 运行和等待实际上是做什么的?What do jasmine runs and waitsFor actually do?(jasmine 运行和等待实际上是做什么的?)
如何提供模拟文件来更改 <input type='filHow to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模拟文件来更改 lt;input type=filegt; 的事
如何使用 Jasmine 对链式方法进行单元测试How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
如何将 $rootScope 注入 AngularJS 单元测试?How do I inject $rootScope into an AngularJS unit test?(如何将 $rootScope 注入 AngularJS 单元测试?)
Jasmine - 如何监视函数中的函数调用?Jasmine - How to spy on a function call within a function?(Jasmine - 如何监视函数中的函数调用?)