小码问答,有问必答!

点击div,打开文件选择对话框,选择上传文件

WEB前端

收藏

1个回答

我要回答

  • author
    码自答 2021-03-08 11:10
    <html>
        <head>
            <title>万码学堂wanmait</title>
            <meta http-equiv="author" content="万码学堂wanmait">
            <style type="text/css">
                #file{
                    display: none;
                }
                
                #div{
                    width: 300px;
                    height: 50px;
                    background-color: aquamarine;
                    border: 1px solid black;
                    border-radius: 10px;
                    text-align: center;
                }
            </style>
        </head>
        <body>
            <div id="div" onclick="openFile()">
               上传文件
                <input id="file" type="file">
            </div>
            
            <script type="text/javascript">
                function openFile()
                {
                    document.getElementById("file").click();
                }
            </script>
        </body>
    </html>