HTML onkeydown 事件屬性


使用者按鍵盤上的鍵時,觸發 HTML onkeydown 事件屬性。

語法

語法如下 −

<tagname onkeydown=”script”>Content</tagname>

我們來看看 HTML onkeydown 事件屬性示例 −

示例

 即時演示

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
   }
   .show {
      font-size: 1.2rem;
   }
</style>
</head>
<body>
<h1>HTML onkeydown Event Attribute Demo</h1>
<textarea class="" placeholder="Enter your message here" rows="5" cols="40" onkeydown="keyFn()"></textarea>
<div class="show"></div>
<script>
   function keyFn() {
      document.querySelector(".show").innerHTML = 'You enter: ' + document.querySelector("textarea").value;
   }
</script>
</body>
</html>

輸出

在白色文字區域輸入你的訊息以觀察 onkeydown 事件屬性如何工作。

更新於: 2019 年 9 月 26 日

127 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.