event.which

event.which 1.1.3追加

戻り値:Number

キーボードまたはマウスイベント用です。押されたボタンに紐付けられたキーコードを取得することが出来ます。

デモ

押されたキーコードを表示します。

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<input size="40" id="whichkey" value="キーボード入力してください">
<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="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<input size="40" id="whichkey" value="マウスボタンを押してください">
<div id="log"></div>
<script>
$('#whichkey').bind('mousedown',function(e){
  $('#log').html(e.type + ': ' +  e.which );
});
</script>

</body>
</html>

 Back to top

© 2010 - 2017 STUDIO KINGDOM