总有人间一两风,填我十万八千梦

jQuery .eventwhich 用法 手册 | 示例代码

jQuery开发手册 归档 196℃ 0评论

描述: 针对键盘和鼠标事件,这个属性能确定你到底按的是哪个键。

  • 添加的版本: 1.1.3event.which

event.whichevent.keyCodeevent.charCode 标准化了。推荐用 event.which 来监视键盘输入。更多细节请参阅: event.charCode on the MDC.

event.which也将正常化的按钮按下(mousedownmouseupevents),左键报告1,中间键报告2,右键报告3。使用event.which代替event.button

例子:

Example: 记录按键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input id="whichkey" value="type something">
<div id="log"></div>
<script>$('#whichkey').bind('keydown',function(e){
$('#log').html(e.type + ': ' + e.which );
}); </script>
</body>
</html>

Example: Log which mouse button was depressed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input id="whichkey" value="type something">
<div id="log"></div>
<script>
$('#whichkey').bind('mousedown',function(e){
$('#log').html(e.type + ': ' + e.which );
});
</script>
</body>
</html>

转载请注明:悠然品鉴 » jQuery .eventwhich 用法 手册 | 示例代码

喜欢 (0)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址