下面的JS代码监听文本框的onkeydown和onkeyup事件,分别在键盘按下和弹起时触发,并定义setStatus函数响应键盘事件
<script type="text/javascript"> function setStatus(name,evt) { evt = (evt) ? evt : ((event) ? event : null); /* ie or standard? */ var charCode = evt.charCode; var status = document.getElementById("keyteststatus"); var text = name +": "+evt.keyCode; status.innerHTML = text; status.textContent = text; } </script> <form> <input type="text" name="keytest" size="1" value="" onkeyup="setStatus('keyup',event)" onkeydown="setStatus('keydown',event)" /> <p id="keyteststatus">status</p> </form>