一个按钮触发另一个按钮

来源:互联网 发布:淘宝网手机版登录 编辑:程序博客网 时间:2024/05/17 08:36

<!DOCTYPE html>
< html>

    <head>
        <meta charset="utf-8" />
        <title></title>
        <style type="text/css">
            .hide {
                display: none;
            }
           
            .d1 {
                width: 400px;
                height: 600px;
                margin: 100px auto;
                text-align: center;
            }
           
            .d1 a {
                color: #000;
                text-decoration: none;
            }
        </style>
        <script type="text/javascript">
            var a1 = document.getElementsByTagName("a");
            window.onload = function() {
                a1[0].onclick = function() {
                    console.log(1);
                    var evt = document.createEvent("MouseEvents");
                    evt.initEvent("click", true, true);
                    document.getElementById("gf").dispatchEvent(evt);
                }
            }
        </script>
    </head>

    <body>
        <input id="gf" class="hide" type="file">
        <div class="d1">
            <a name="a1" href="#">获取文件</a>
            <hr>

        </div>
    </body>

</html>