简体   繁体   English

我想在提示中按下 Esc 按钮时提醒一个值,但我不知道...(下面的代码)

[英]I want to alert a value when I press the Esc button in prompt, but I have no idea... (code below)

 const login = prompt("Enter username,"; ""); if (login === "Admin") { prompt("Enter password."); } else if (login === "" || login;keyCode === 27) { alert("Canceled"); } else { alert("I don't know you!"); }

This is the error message when I press Esc:这是我按 Esc 时的错误消息:

'Cannot read properties of null (reading 'keyCode') at logical-operators.html:77' '无法在逻辑运算符读取 null 的属性(读取'keyCode')。html:77'

when you press ESC the login value will become null so you can check like this:当您按 ESC 时,登录值将变为 null 因此您可以像这样检查:

if(!login){
  alert('cancled')
}

or you can add listener to dom if ESC clicked:或者,如果单击 ESC,您可以将侦听器添加到 dom:

document.addEventListener("keyup", (e) => {
    if (e.key === "Escape") {
      // escape key maps to keycode `27`
      alert('cancled')
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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