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

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

แอตทริบิวต์เหตุการณ์ oninput ใน HTML ถูกกระตุ้นเมื่อผู้ใช้ป้อนข้อมูลในองค์ประกอบ input/textarea HTML ในเอกสาร HTML

ไวยากรณ์

<tagname oninput="script"></tagname>

ตัวอย่าง

<!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; }
  textarea { border: 2px solid #fff; background: transparent; font-size: 1rem; }
  ::placeholder { color: #000; font-size: 1rem; }
</style>
</head>
<body>
<h1>ตัวอย่างแอตทริบิวต์เหตุการณ์ oninput ของ HTML</h1>
<textarea placeholder="ป้อนข้อความที่นี่" oninput="inputFn()" rows='8' cols="50"></textarea>
<script>
  function inputFn() { document.querySelector('textarea').style.background = '#ffffff36'; }
</script>
</body>
</html>

ผลลัพธ์

ลองพิมพ์ข้อความใน text area เพื่อดูการทำงานของ oninput

พื้นหลัง textarea จะเปลี่ยนเป็นสีขาวโปร่งแสงเมื่อเริ่มพิมพ์