jQuery keydown()Method

jQuery 事件方法jQuery Event Methods

Example

Set the background color of an <input> field when a keyboard key is pressed:

$("input").keydown(function(){ $("input").css("background-color","yellow"); });

Try it Yourself »

Definition and Usage

Event order related to the keydown event:

  1. keydown - The process of pressing a key
  2. keypress- The key is pressed
  3. keyup- The key is released

The keydown event occurs when a keyboard key is pressed.

The keydown() method triggers the keydown event, or specifies a function to run when a keydown event occurs.

Tip:Please useevent.whichproperty to return which keyboard key was pressed.


Syntax

Trigger the keydown event for the selected element:

$(selector).keydown() Try it Yourself

Add a function to the keydown event:

$(selector).keydown(function) Try it Yourself

Parameter Description
function Optional. Specifies the function to run when the keydown event is triggered.


实例

More Examples

Determine which key is pressed
How to use the event.which property to determine which key is pressed.


jQuery 事件方法jQuery Event Methods

Other Extensions