如何调用“;点击“;输入类型“on input type”;文件“;通过调用其他元素的上下文

How to call "click" on input type "file" by calling context on other element?

本文关键字:调用 文件 其他 上下文 元素 type 类型 何调用 点击 on 输入      更新时间:2023-09-26

如何通过调用其他元素的"context"事件来调用input type="file"上的"click"事件?

我正在尝试这个代码:

HTML标记:

<html>
    <head>
        <title>Title</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="js.js"></script>
</head>
<body>
    <input type="file" id="file"/>
    <button id="trigger">Click</button>
</body>

JavaScript文件:

window.onload = function() {
window.oncontextmenu = function(){
    return false;
};
$("#trigger").on("contextmenu", function(){
    $("#file").trigger("click");
});

}

但我没有窗口来选择文件,当我用鼠标右键点击id="trigger". 按钮时

已解决!

  window.onload = function() {
        $("#trigger").mousedown(function(e){
            if(e.button == 2){
                $("#file").trigger("click");
            }
        });
    }

这样尝试:

脚本:

$('#trigger').click(function() {
    $('#file').trigger('click');
});