Web页面监听回车键JS

if (document.addEventListener) {
	//如果是Firefox  
	document.addEventListener("keypress", enterEvent, true);
} else {
	//如果是IE
	document.attachEvent("onkeypress", enterEvent);
}
function enterEvent(evt) {
	if (evt.keyCode == 13) {
		//do something
	}
}
如果想针对某一元素添加监听回车使用如下方法:
$(document).ready(function(){
         $('#thirdPartyOrderNumber').focus(function(){
                 if (document.addEventListener) {
                         //如果是Firefox  
                         document.addEventListener("keypress", enterEvent, true);
                 } else {
                         //如果是IE
                         document.attachEvent("onkeypress", enterEvent);
                 }
         });
         $('#thirdPartyOrderNumber').blur(function(){
                 if (document.addEventListener) {
                         //如果是Firefox  
                         document.removeEventListener("keypress", enterEvent, true);
                 } else {
                         //如果是IE
                         document.detachEvent("onkeypress", enterEvent);
                 }
         });
});

你可能感兴趣的:(回车,监听回车键,Web回车键)