这个有效:
var picdrag = document.getElementById('picdrag');
picdrag.addEventListener('drop', picSelect, false);
function picSelect(e) {
var pics = e.dataTransfer.files;
}
这不是:
$('#picdrag').on('drop', function(e) {picSelect(e);});
function picSelect(e) {
var pics = e.dataTransfer.files;
}
因为它报告错误'e.dataTransfer is undefined'
。当我不知道为什么某些东西有效或无效时,我讨厌它。这是特定于 drop 事件的,就像 jQuery 以不同方式处理它一样。
最佳答案
你可以试试:
var pics = e.originalEvent.dataTransfer.files;
来自docs :
Certain events may have properties specific to them. Those can be accessed as properties of the event.originalEvent object. To make special properties available in all event objects, they can be added to the jQuery.event.props array. This is not recommended, since it adds overhead to every event delivered by jQuery.
还有一个应该感兴趣的例子:
// add the dataTransfer property for use with the native `drop` event
// to capture information about files dropped into the browser window
jQuery.event.props.push("dataTransfer");
关于Javascript:如何在 jQuery 中编写这个放置事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226636/