2023-12-20 16:03

点击div-打开文件选择对话框

码自答

WEB前端

(136)

(0)

收藏

表单的文件域file隐藏

div加点击事件

点击div调用文件域file的click()方法


代码如下:

<html>
    <head>
        <title>万码学堂wanmait</title>
    </head>
    <style type="text/css">
        #file{
            display: none;
        }
        #div{
            width: 300px;
            height: 50px;
            border: 1px solid black;
            border-radius: 10px;
            text-align: center;
        }
    </style>
    <body>
        <div id="div" onclick="openFile()">
            上传文件
            <input type="file" id="file">
        </div>
        <script type="text/javascript">
            function openFile()
            {
                document.getElementById("file").click();
            }
        </script>
    </body>
</html>


0条评论

点击登录参与评论