文件上传 JS + JQ 将文件拖入对应框内进行上传

72 阅读1分钟

JS + JQ 实现拖入文件上传

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="one" style="width: 300px;height: 100px;border: 1px solid red"></div>
    <div class="one" style="width: 300px;height: 100px;border: 1px solid red"></div>
    <div class="one" style="width: 300px;height: 100px;border: 1px solid red"></div>
    <div class="one" style="width: 300px;height: 100px;border: 1px solid red"></div>
    
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        var num = document.getElementsByClassName('one')
            
        $(".one").mouseover(function () {
        	var m = $(".one").index(this)
			// console.log(m);
            num[m].ondragover = function(e) {
                e.preventDefault();
            }
            num[m].ondrop = function(e) {
                e.preventDefault()
                console.log(m);
                console.log(num[m]);
            console.log(e.dataTransfer.files[0]);
            
        }
        })

    </script>
</body>
</html>