2015-10-21 51 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; 
});