我在事件中传递给函数的事件对象有问题drop
。在我的代码中,div#dropArea
它的 drop 事件是否由firstDrop
执行一些动画的函数处理,然后调用dropFromDesktop
处理e.dataTransfer.files
对象的正确函数。我需要在两个单独的函数中使用这种方法,因为 HTML 文档中的其他一些 div 也进一步使用了后者(无需复制代码)。第一个仅使用一次,以隐藏一些“欢迎”文本。
通常,这种机制允许您从桌面拖动文件并将它们放到我网站上的某个区域中。
这是它的外观(在快捷方式中):
function firstDrop(ev) {
var $this = $(this);
//when I call the function here, it passes the event with files inside it
//dropFromDesktop.call($this, ev);
$this.children('.welcomeText').animate({
opacity: '0',
height: '0'
}, 700, function() {
$('#raw .menu').first().slideDown('fast', function() {
//when I call the function here, it passes the event, but 'files' object is empty
dropFromDesktop.call($this, ev);
});
});
}
function dropFromDesktop(ev) {
var files = ev.originalEvent.dataTransfer.files;
(...) //handling the files
}
$('#dropArea').one('drop', firstDrop);
$('some_other_div').on('drop', dropFromDesktop);
问题出在jQuery.animation
的回调中 - 当我在其中调用我的函数时,事件对象被正确传递,但来自 dataTransfer 的文件对象为空!
整个脚本都放在里面$(document).ready(function() { ... });
,所以函数声明的顺序并不重要,我猜。