簡體   English   中英

在按下另一個按鈕時觸發鼠標單擊

[英]Fire a mouseclick on a press of another button

如何通過按鍵盤上的“輸入”按鈕或其他按鈕組合來觸發頁面上某個元素的鼠標單擊?

嘗試以下代碼:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
    <title>Ilan's Test</title>
</head>

<body>

    <div class="container">
        <div class="row">
            <div class="col-lg-12" id="results">

            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>

    <script>
        // We begin listening to keypresses on the document body
        $(document).keypress(function(e) {
            // If the key pressed was Enter (keycode 13)
            if (e.keyCode == 13) {
                // Perform an action -- You can now envoke a click event on an element's ID for example, here I just console log and append text to my results div
                $('#results').append('<div>You hit enter!</div><hr>');
                // Console log
                console.log('you pressed enter');
                return false;
            }
        });
    </script>
</body>

</html>

我用香草JS弄清楚了

window.onkeyup = function(e) {
    var key = e.keyCode   
    if (key == 13) {
      document.getElementById('test').click()
    }
}

這將監視何時有人按下按鈕,然后檢查他們是否按下了回車鍵;如果有人按下了按鈕,它將在ID為“ test”的元素上進行單擊。

simulate(document.getElementById("btn"), "click");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM