我如何获得 .which 方法来处理 and 语句?

How do I get the .which method to work with an and statement?

我想确保我的 body 仅在按下字母时淡出。使用当前代码,当我按下给定范围之间的键时,什么也不会发生。即使它只是带有 event.which === 65 的普通语句,当我按下值为“A”的值 65 时,也没有任何反应。我以前用过这个 属性 并使它工作,但我没有看到任何语法差异。是因为我的选择吗?无论如何,这是 Javascript/jQuery 代码。提前致谢。

$(document).ready(function(){
    $("body").hide()
    $("body").fadeIn(3000)
})

$(document).on("keypress", function(event){
    if(event.which ===65){
        $("body").fadeOut(1500)
    }
})

我刚刚测试了以下代码,它工作正常。您的错误不在您向我们展示的样本中。

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<p>Here is the body</p>
</body>
</html>
<script>
$(document).ready(function(){
    $("body").hide()
    $("body").fadeIn(3000)
})

$(document).on("keypress", function(event){
    if(event.which ===65){
        $("body").fadeOut(1500)
    }
})
</script>

因此按键事件区分大小写,因此 'a' 的代码是 97 而不是 65。请尝试改用此代码:

$(document).on("keydown", function(event){
if(event.which ===65){
    $("body").fadeOut(1500)
}
})

还要弄直分号