Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

HTML onkeydown แอตทริบิวต์เหตุการณ์


แอตทริบิวต์เหตุการณ์ HTML onkeydown ถูกทริกเกอร์เมื่อผู้ใช้กดปุ่มบนแป้นพิมพ์

ไวยากรณ์

ต่อไปนี้เป็นไวยากรณ์ -

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

ให้เราดูตัวอย่างของ HTML onkeydown event Attribute -

ตัวอย่าง

<!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>

ผลลัพธ์

HTML onkeydown แอตทริบิวต์เหตุการณ์

ป้อนข้อความของคุณในพื้นที่ข้อความสีขาวเพื่อดูว่าแอตทริบิวต์เหตุการณ์ onkeydown ทำงานอย่างไร

HTML onkeydown แอตทริบิวต์เหตุการณ์