2015-10-21 49 views
0

当前我的网页启用了按键检测功能,当按A和D时,它会改变图像(图像源)。我只是添加了登录,注册功能,但是当我输入用户名或密码时,有时会打A和D,图片会发生变化,我不想要,如何在登录元素关注时禁用图片更改功能?如何在关注某个HTML元素时禁用jQuery按键检测?

我在想,既然文档选择器选择整个页面,有没有办法排除某些元素?还是有另一种方法来实现我想要的?

<div id="loginContainer"> 
Username:<br/> <input type="text" name="username"/><br/> 
Password:<br/> <input type="password" name="password"/><br/> 
<input type="button" name="login" value="Login"/> 
</div> 

---------- 

$(document).keypress(function(e){ 
     // key press detection code here 
}); 

感谢您提前给予的帮助。

回答

1

document.activeElement return focused item。你可以检查它是否是确定的元素。在每个jQuery的funtion如果你return false;它的作用就像禁用功能

$(document).keypress(function(e){ 
     if(document.activeElement && document.activeElement != document.body) 
      return false; 
     // key press detection code here 
}); 
0

你可以只检查元素集中,并返回:

$(document).keypress(function(e){ 
    if($("input[name='login']").is(":focus")) return; 
    // key press detection code here 
}); 
0
$(document).keypress(function(e){ 
    e.stopImmediatePropagation(); 
    return false; 
});