<html>
<body>
<p>本例使用 addEventListener() 方法将 "keypress" 事件附加到 input 元素。</p>
<p>在文本字段内按一个键可设置红色背景颜色。</p>
<input type="text" id="demo">
<script>
document.getElementById("demo").addEventListener("keypress", myFunction);
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>