jquery按键事件_jQuery按键事件

jquery按键事件

jQuery keypress is one of the keyboard event methods. Other jQuery keyboard event methods are keyup and keydown.

jQuery keypress是键盘事件方法之一。 其他jQuery键盘事件方法是keyup和keydown。

jQuery按键 (jQuery keypress)

jQuery keypress method triggers with key press event of the javascript. Since this event varies across browsers, this is not covered in any official specification. This event triggers only for the focused element. This can vary across different browsers.

jQuery keypress方法使用javascript的按键事件触发。 由于此事件在不同的浏览器中会有所不同,因此任何正式规范均未涵盖。 此事件仅针对焦点元素触发。 跨不同的浏览器可能会有所不同。

jQuery keypress方法语法 (jQuery keypress method syntax)

  1. keypress(): This jQuery keypress method is used without any arguments.

    keypress():此jQuery keypress方法使用时不带任何参数。
  2. keypress(handler): This jQuery keypress method attaches a function to be executed when the keypress event is fired.

    keypress(handler):此jQuery keypress方法附加了一个在触发keypress事件时要执行的功能。

jQuery按键示例 (jQuery keypress example)

Following example demonstrates the use of jQuery keypress method.

以下示例演示了jQuery keypress方法的使用。

jquery-keypress.html

jquery-keypress.html

<!DOCTYPE html>
<html>
<head>
<title>jQuery keypress event example</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
  $("input:text").keyup(function(){
    $("input:text").css("background-color","red");
  });
$("input:text").keypress(function(){
    $("input:text").css("background-color","yellow");
  });
  //reset jQuery keydown css
  $("input:reset").click(function(){
    $("input:text").css("background-color","white");
  });
});
</script>
</head>
<body>
<h3>jQuery keypress example</h3>
Enter anything: <input type="text">
<input type="reset">
<br><br>
Type something above, background colour will change to yellow on key press.
<br><br>
Click on Reset button to try again.
</body>
</html>

In above example, when the key is pressed, keypress() method is triggered which in turn executes the function that changes the background colour to yellow.

在上面的示例中,当按下键时,将触发keypress()方法,该方法又执行将背景色更改为黄色的功能。

Below screencast shows above jQuery keypress event example in action.

下面的截屏视频显示了上面的jQuery keypress事件示例。

jQuery按键演示 (jQuery keypress demo)

演示地址

翻译自: https://www.journaldev.com/13179/jquery-keypress-event

jquery按键事件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值