返回值:Numberevent.which

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

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

event.which 同样对鼠标按键进行了标准化 (mousedownmouseup 事件), 1 代表左键, 2 代表中键, 3 代表右键。请使用 event.which 替代 event.button

示例:

记录哪个键盘按键被按下。

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.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>

演示:

示例:

记录哪个鼠标键被按下。

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.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>

演示: