x
 
<!DOCTYPE html>
<html>
<body>
<p>本例演示如何将 "onkeypress" 事件分配给 input 元素。</p>
<p>Press a key inside the text field to set a red background color.</p>
<input type="text" id="demo" onkeypress="myFunction()">
<script>
function myFunction() {
  document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>