为什么无法删除文件才能与jquery一起使用? [英] Why can't I get file dropping to work with jquery?

查看:93
本文介绍了为什么无法删除文件才能与jquery一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何拖放文件.我已经阅读了许多教程和示例,并认为我知道该怎么做,但是我无法使绝对的基本部分正常工作.

I am trying to figure out how to do file drag and drop. I have read a number of tutorials and examples and think I know how to do it, but I can't get the absolute basic part working.

这是我当前拥有的代码.如果我将文件拖到filedropper div上,浏览器将仅加载该文件,而不是发出删除警报.我想念什么?

Here's the code I have currently. If I drag a file onto the filedropper div, the browser just loads the file instead of giving me the drop alert. What am I missing?

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
</head>
<body>
    <script language='javascript'>
    $(document).ready(function() {
        $('#filedropper').on('drop', function(e){
            e.preventDefault();
            alert('drop');
            return false;
        });
    });
    </script>
    <div id='filedropper' style='height:100px;width:100px;background:yellow'></div>
</body>
</html>

推荐答案

首先,如果您使用的是IE,则可能根本无法使用. IE(可能不是IE 10)不支持文件拖放.

First, if you're using IE, this likely won't work at all. IE (other than maybe IE 10) doesn't support file drag and drop.

第二,您需要捕获并阻止$(document)中的dragOver函数.就绪:

Second, you need to catch and prevent the dragOver function in your $(document).ready:

$('#filedropper').on('dragover', function(e) {
    if (e.stopPropagation) { e.stopPropagation(); } // The if checks are excessive but safest
    if (e.preventDefault) { e.preventDefault(); }
});

是我在学习如何做时的主要参考

This was my primary reference when I was learning how to do it.

那应该带你走.

在JsBin上查看

这篇关于为什么无法删除文件才能与jquery一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆