Jquery给HTML元素绑定按键事件-回车事件

来源:互联网 发布:js 后退返回网址 编辑:程序博客网 时间:2024/05/16 14:38

http://blog.csdn.net/lllliulin/article/details/50445331


在做网页的时候,有时是需要js来判断用户的按键来进行操作对应的功能。 
本实例已加载jquery.js

1、全局判断按键(按回车)

$(document).keypress(function(e) {       var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;        if (eCode == 13){            alert('您按了回车键')            //自己写判断操作        }});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、给某个元素(按回车)

$("input[name=keyword]").keypress(function(e){        var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;        if (eCode == 13){            alert('您按了回车键')            //自己写判断函数        }})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里的13对应的键就是回车键。

以下是组合按键的应用:

if(e.altKey && e.keyCode==13){    alert('您同时按了Alt键和Enter键')        //ctrl键是e.ctrlKey        //shift键是e.shiftKey    }});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以下是单个按键对应的数字,建议使用keyup,keypress在某些浏览器不生效:

keycode   8 = BackSpace BackSpacekeycode   9 = Tab Tabkeycode 12 = Clearkeycode 13 = Enterkeycode 16 = Shift_Lkeycode 17 = Control_Lkeycode 18 = Alt_Lkeycode 19 = Pausekeycode 20 = Caps_Lockkeycode 27 = Escape Escapekeycode 32 = space spacekeycode 33 = Priorkeycode 34 = Nextkeycode 35 = Endkeycode 36 = Homekeycode 37 = Leftkeycode 38 = Upkeycode 39 = Rightkeycode 40 = Downkeycode 41 = Selectkeycode 42 = Printkeycode 43 = Executekeycode 45 = Insertkeycode 46 = Deletekeycode 47 = Helpkeycode 48 = 0 equal bracerightkeycode 49 = 1 exclam on esuperiorkeycode 50 = 2 quotedbl twosuperiorkeycode 51 = 3 section threesuperiorkeycode 52 = 4 dollarkeycode 53 = 5 percentkeycode 54 = 6 ampersandkeycode 55 = 7 slash braceleftkeycode 56 = 8 parenleft bracketleftkeycode 57 = 9 parenright bracketrightkeycode 65 = a Akeycode 66 = b Bkeycode 67 = c Ckeycode 68 = d Dkeycode 69 = e E EuroSignkeycode 70 = f Fkeycode 71 = g Gkeycode 72 = h Hkeycode 73 = i Ikeycode 74 = j Jkeycode 75 = k Kkeycode 76 = l Lkeycode 77 = m M mukeycode 78 = n Nkeycode 79 = o Okeycode 80 = p Pkeycode 81 = q Q atkeycode 82 = r Rkeycode 83 = s Skeycode 84 = t Tkeycode 85 = u Ukeycode 86 = v Vkeycode 87 = w Wkeycode 88 = x Xkeycode 89 = y Ykeycode 90 = z Zkeycode 96 = KP_0 KP_0keycode 97 = KP_1 KP_1keycode 98 = KP_2 KP_2keycode 99 = KP_3 KP_3keycode 100 = KP_4 KP_4keycode 101 = KP_5 KP_5keycode 102 = KP_6 KP_6keycode 103 = KP_7 KP_7keycode 104 = KP_8 KP_8keycode 105 = KP_9 KP_9keycode 106 = KP_Multiply KP_Multiplykeycode 107 = KP_Add KP_Addkeycode 108 = KP_Separator KP_Separatorkeycode 109 = KP_Subtract KP_Subtractkeycode 110 = KP_Decimal KP_Decimalkeycode 111 = KP_Divide KP_Dividekeycode 112 = F1keycode 113 = F2keycode 114 = F3keycode 115 = F4keycode 116 = F5keycode 117 = F6keycode 118 = F7keycode 119 = F8keycode 120 = F9keycode 121 = F10keycode 122 = F11keycode 123 = F12keycode 124 = F13keycode 125 = F14keycode 126 = F15keycode 127 = F16keycode 128 = F17keycode 129 = F18keycode 130 = F19keycode 131 = F20keycode 132 = F21keycode 133 = F22keycode 134 = F23keycode 135 = F24keycode 136 = Num_Lockkeycode 137 = Scroll_Lockkeycode 187 = acute gravekeycode 188 = comma semicolonkeycode 189 = minus underscorekeycode 190 = period colonkeycode 192 = numbersign apostrophekeycode 210 = plusminus hyphen macronkeycode 212 = copyright registeredkeycode 213 = guillemotleft guillemotrightkeycode 214 = masculine ordfemininekeycode 215 = ae AEkeycode 216 = cent yenkeycode 217 = questiondown exclamdownkeycode 218 = onequarter onehalf threequarterskeycode 220 = less greater barkeycode 221 = plus asterisk asciitildekeycode 227 = multiply divisionkeycode 228 = acircumflex Acircumflexkeycode 229 = ecircumflex Ecircumflexkeycode 230 = icircumflex Icircumflexkeycode 231 = ocircumflex Ocircumflexkeycode 232 = ucircumflex Ucircumflexkeycode 233 = ntilde Ntildekeycode 234 = yacute Yacutekeycode 235 = oslash Oobliquekeycode 236 = aring Aringkeycode 237 = ccedilla Ccedillakeycode 238 = thorn THORNkeycode 239 = eth ETHkeycode 240 = diaeresis cedilla currencykeycode 241 = agrave Agrave atilde Atildekeycode 242 = egrave Egravekeycode 243 = igrave Igravekeycode 244 = ograve Ograve otilde Otildekeycode 245 = ugrave Ugravekeycode 246 = adiaeresis Adiaeresiskeycode 247 = ediaeresis Ediaeresiskeycode 248 = idiaeresis Idiaeresiskeycode 249 = odiaeresis Odiaeresiskeycode 250 = udiaeresis Udiaeresiskeycode 251 = ssharp question backslashkeycode 252 = asciicircum degreekeycode 253 = 3 sterlingkeycode 254 = Mode_switch
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146

Javascript中 有3个事件句柄在对应键盘的输入状态:keydown、keypress和keyup。 
分别对应的意思是:按键被按下(按下按键但还没有抬起)、点击按键(按下并抬起按键)、按键抬起(按键抬起之后)

按键的分类:

按键可以分为“实键”和“虚键” 
实键可以理解为我们能够看到并打印出来的按键,如字母“A”、数字“1”、字符“?”等等 
虚键就是那些无法打印出来起到控制作用的按键,如“Ctrl”、“Alt”、“Shift”、“方向键”等等 
IE在处理虚键时有个特例:虚键不会产生keypress事件,必须使用keydown或keyup来捕获

按键码和字符码

按键码是计算机用来识别不同按键的编码,每一个按键都有按键码 
字符码是可被打印的实键特有的,对应了键盘上显示的字符 
按键码可以使用String.fromCharCode()转换为字符码 
按键码和和字符码的对应表可以在本文最后找到。

获取实键

$('body').bind('keypress',getKeyCode);   function getKeyCode(e) {      var evt = e || window.event;      var keyCode = evt.keyCode || evt.which || evt.charCode;      alert(keyCode);  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

稍微解释下 
1. e为Firefox等标准浏览器支持的JS隐藏变量,表示一个“事件”;IE系列中没有“e”,而是用window.event来表示“事件”;所以var e = e || window.event;就表示:获取当前正在发生的事件。 
2. e.keyCode、e.which、e.charCode都代表获取按键码,但不同的浏览器支持不同的写法

获取虚键

function getKeyCode(e) {      var evt = e || window.event;      if (evt.ctrlKey) alert("ctrlKey pressed");      if (evt.altKey) alert("altKey pressed");      if (evt.shiftKey) alert("shiftKey pressed");  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

字母和数字键的键码值(keyCode)

这里写图片描述
这里写图片描述
这里写图片描述


原创粉丝点击