jQuery KeyUp()问题 [英] jQuery KeyUp() Issue

查看:99
本文介绍了jQuery KeyUp()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦用户按下a,按钮颜色将会改变,然后一旦用户释放a按钮,它应该再次更改。一旦按下字母a,按钮只会更改颜色,而不是在被释放时。

Once the user preses "a", the button colour will change, then once the user releases the "a" button it should change again. The button only changes colour once the letter "a" has been pressed, not when released.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>website</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
    $(document).keyup(function(event) {
        if ( event.which == 97){
            $('.button0 input').css('color', 'rgb(0, 0, 255)');
        }
    });
    $(document).keypress(function(event) {
        if ( event.which == 97){
            $('.button0 input').css('color', 'rgb(128, 0, 0)');
        }
    });
});
</script>
<style type="text/css">
.button0 input{
    position:fixed;
    left:41px;
    top:12px;
    font-family:Arial;
    font-size:8px;
    font-weight:normal;
}
</style>
<body>
    <div class="button0">
        <input type="button" style="width: 303px;height: 165px;" value="Button"/>
    </div>
</body>
</html>


推荐答案

jQuery API页面 for .keypress()


请注意,keydown和keyup提供一个代码,指示按哪个键,而按键指示输入哪个字符。例如,一个小写的a将被keydown和keyup报告为65,而按按键按97。所有事件报告为大写A为65。因为这个区别,当捕捉到特殊的按键,如箭头键,.keydown()或.keyup()是一个更好的选择。

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

所以你需要将 event.which == 97 更改为 event.which == 65 在你的 keyup 处理程序。

So you need to change event.which == 97 to event.which == 65 in your keyup handler.

这篇关于jQuery KeyUp()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆