jquery keydown() method


  Translation results:

keydown

Keyboard press


jquery keydown() methodsyntax

Function:The complete key press process is divided into two parts: 1. The key is pressed; 2. The key is released. When the button is pressed, the keydown event occurs. The keydown() method triggers the keydown event, or specifies a function to run when the keydown event occurs. If set on a document element, this event occurs regardless of whether the element has focus. Please use the .which property to determine which key was pressed (try it yourself).

Trigger the keydown event syntax: $(selector).keydown()

Bind the function to the keydown event syntax: $ (selector).keydown(function)

Parameters:

##ParametersDescriptionfunction Optional. Specifies the function to run when the keydown event occurs.​

jquery keydown() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("input").keydown(function(){
    $("input").css("background-color","#FFFFCC");
  });
  $("input").keyup(function(){
    $("input").css("background-color","#D6D6FF");
  });
});
</script>
</head>
<body>
Enter your name: <input type="text" />
<p>当发生 keydown 和 keyup 事件时,输入域会改变颜色。请试着在其中输入内容。</p>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance