admin管理员组文章数量:1391951
I am learning how to add an EventListener
to a <div>
so that I can drop a file to it for reading a csv file. Below is my simple HTML code that adds an EventListener
, but when I dragged and dropped a file, the Chrome browser keeps downloading the file that I dropped and the console output "hey" is not printed. If anyone could let me know what is wrong I'd greatly appreciate the help. :)
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
#drop{
border:2px dashed #bbb;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
padding:25px;
text-align:center;
width:128px;
font:20pt bold,"Vollkorn";color:#bbb
}
</style>
</head>
<body>
<div id="drop">Drop a file here</div>
<script type="text/javascript">
var drop_dom_element = document.getElementById('drop');
var workbook;
/* set up drag-and-drop event */
function handleDrop(e) {
console.log('hello');
e.stopPropagation();
e.preventDefault();
console.log('hey');
}
drop_dom_element.addEventListener('drop', handleDrop, false);
console.log('Done');
</script>
</body>
</html>
I am learning how to add an EventListener
to a <div>
so that I can drop a file to it for reading a csv file. Below is my simple HTML code that adds an EventListener
, but when I dragged and dropped a file, the Chrome browser keeps downloading the file that I dropped and the console output "hey" is not printed. If anyone could let me know what is wrong I'd greatly appreciate the help. :)
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
#drop{
border:2px dashed #bbb;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
padding:25px;
text-align:center;
width:128px;
font:20pt bold,"Vollkorn";color:#bbb
}
</style>
</head>
<body>
<div id="drop">Drop a file here</div>
<script type="text/javascript">
var drop_dom_element = document.getElementById('drop');
var workbook;
/* set up drag-and-drop event */
function handleDrop(e) {
console.log('hello');
e.stopPropagation();
e.preventDefault();
console.log('hey');
}
drop_dom_element.addEventListener('drop', handleDrop, false);
console.log('Done');
</script>
</body>
</html>
Share
Improve this question
asked Apr 11, 2016 at 12:32
user1330974user1330974
2,6165 gold badges40 silver badges72 bronze badges
1 Answer
Reset to default 7The default behaviour of most (all?) elements is not to allow dropping. Read this MDN article.
A listener for the dragenter and dragover events are used to indicate valid drop targets, that is, places where dragged items may be dropped. Most areas of a web page or application are not valid places to drop data. Thus, the default handling of these events is not to allow a drop.
Adding event.preventDefault()
to the dragover
event seems to do the trick in current versions of Firefox and Chrome.
element.addEventListener("dragover", function( event ) {
// prevent default to allow drop
event.preventDefault();
}, false);
Under Performing a Drop:
When the user releases the mouse, the drag and drop operation ends. If the mouse was released over an element that is a valid drop target, that is, one that cancelled the last dragenter or dragover event, and then the drop will be successful, and a drop event will fire at the target. Otherwise, the drag operation is cancelled, and no drop event is fired.
The reason for adding the preventDefault
call to the drop
event function too is to prevent the default behaviour browsers might have such as immediately opening links and images.
You can see in this jsfiddle that dragging text or images will trigger the drop
event, but Firefox and Chrome will also open images and links.
本文标签: javascriptAdding eventListener for file dropStack Overflow
版权声明:本文标题:javascript - Adding eventListener for file drop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744734297a2622231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论