标签:
1.第一种写法(监听整个页面的回车事件)
document.onkeydown=keyDownSearch; function keyDownSearch(e) { // 兼容FF和IE和Opera var theEvent = e || window.event; var code = theEvent.keyCode || theEvent.which || theEvent.charCode; if (code == 13) { alert(‘回车‘);//具体处理函数 return false; } return true; }
2.第二种(是对一个输入框的监听)
function getKey() { if(event.keyCode==13){ alert("回车 "); } }
对应的html代码,如下:
<input type="text" name="username" id="username" onkeypress="getKey();"></input>
标签:
原文地址:http://www.cnblogs.com/charles-kun/p/5487383.html