jQuery忽略第二个条件[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我已经编写了一个jQuery函数,当某个元素具有焦点时,我打算检查何时特定按下shift键。

以下是我编写的代码

$(document).keyup(function (e) {
    if (e.which === 9 && e.shiftKey) {
        if ($(".focus > a").focus) {
            alert("test");
        }
    }
});

但是,无论.focus > a是否具有焦点,当按下shift键时,警报似乎都起作用。我在这里犯了错误还是这个错误?

javascript jquery html
1个回答
0
投票

if ($(".focus > a").focus将检查在focus对象中是否存在定义为$(".focus > a")的属性。由于存在,您的情况始终如此。

所以改为:

if $(".focus > a").is(":focus") .....

Using jQuery to test if an input has focus

© www.soinside.com 2019 - 2024. All rights reserved.