Home  >  Article  >  Web Front-end  >  jquery获得keycode的示例代码_jquery

jquery获得keycode的示例代码_jquery

WBOY
WBOYOriginal
2016-05-16 17:06:12857browse

如下所示:

复制代码 代码如下:

txtSearch:文本框ID

$("#txtSearch").keyup(function (event) {
    var keycode = event.which;
    if (keycode == 13) {
        alert('你已经按下回车键');    

    }

 });

或者

xObj.keyup(function(event){
     //获取当前按键的键值
     //jQuery的event对象上有一个which的属性可以获得键盘按键的键值
     var keycode = event.which;
     //处理回车的情况
     if(keycode==13){

    }
     //处理esc的情况
     if(keycode == 27){

      
     }
 }); 
或者
$(document).keyup(function(event){
     //获取当前按键的键值
     //jQuery的event对象上有一个which的属性可以获得键盘按键的键值
     var keycode = event.which;
     //处理回车的情况
     if(keycode==13){

    }
     //处理esc的情况
     if(keycode == 27){

      
     }
 }); 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn