HTML onkeydown 事件属性

htmlweb developmentfront end technology更新于 2025/12/21 2:52:17

当用户按下键盘上的某个键时,会触发 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 事件属性如何工作。


相关文章


有用资源