简体   繁体   中英

Trigger JavaScript action with keypress

2020 update:

I have a modal that closes with the code modal.style.display = "none"; . How can I, using JavaScript, trigger the above code when the user presses "ESC" on their keyboard?

Original question:

As the title says. Here is the modal code I'm using: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal2

Add this to your javascript code :

window.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 27) {
        modal.style.display = "none";
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM