Skip to content

Instantly share code, notes, and snippets.

@foru17
Created April 3, 2014 15:54
Show Gist options
  • Save foru17/9957088 to your computer and use it in GitHub Desktop.
Save foru17/9957088 to your computer and use it in GitHub Desktop.
js监听键盘回车实例
document.onkeydown = function(e) {
// 兼容FF和IE和Opera
var theEvent = e || window.event;
var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
var activeElementId = document.activeElement.id;//当前处于焦点的元素的id
if (code == 13 && activeElementId == "search_text") {
search();//要触发的方法
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment