簡體   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!"); }

這是我按 Esc 時的錯誤消息:

'無法在邏輯運算符讀取 null 的屬性(讀取'keyCode')。html:77'

當您按 ESC 時,登錄值將變為 null 因此您可以像這樣檢查:

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

或者,如果單擊 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