當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML DOM KeyboardEvent altKey屬性用法及代碼示例

HTML DOM KeyboardEvent altKey 屬性返回ALT 當在 HTML 文檔中觸發按鍵事件時按鍵是否被按下。

用法

以下是語法 -

event.altKey

讓我們看一個 HTML KeyboardEvent altKey 屬性的例子 -

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color:#000;
      height:100vh;
      background:linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align:center;
   }
   input {
      border:2px solid #fff;
      padding:8px;
      background:transparent;
      width:310px;
      border-radius:20px;
      outline:none;
   }
   ::placeholder {
      color:#000;
   }
   .show {
      font-size:1.2rem;
      color:#fff;
   }
</style>
<body>
<h1>HTML KeyboardEvent altKey Property Demo</h1>
<input type="text" placeholder="Enter your message" onkeydown="display(event)">
<div class="show">
</div>
<script>
   function display(event) {
      if (event.altKey) {
         document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
      } else {
         document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
      }
   }
</script>
</body>
</html>

輸出

現在開始按下文本字段中的任意鍵,然後觀察當按下 ALT 鍵時 altKey 屬性如何工作 -

相關用法


注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 HTML DOM KeyboardEvent altKey Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。