这个问题在这里已有答案:
我已经编写了一个jQuery函数,当某个元素具有焦点时,我打算检查何时特定按下shift键。
以下是我编写的代码
$(document).keyup(function (e) {
if (e.which === 9 && e.shiftKey) {
if ($(".focus > a").focus) {
alert("test");
}
}
});
但是,无论.focus >
a是否具有焦点,当按下shift键时,警报似乎都起作用。我在这里犯了错误还是这个错误?
if ($(".focus > a").focus
将检查在focus
对象中是否存在定义为$(".focus > a")
的属性。由于存在,您的情况始终如此。
所以改为:
if $(".focus > a").is(":focus") .....