简体   繁体   English

从某些元素中排除按键

[英]Exclude keypress from certain element

I want to catch spacebar and below did catch it. 我想抓住空格键,下面确实抓住了它。 However it's tied to document, the catch will trigger even if I press spacebar when I'm typing in an input box. 但是,它与文档相关,即使在输入框中键入空格键,捕获也会触发。 How to exclude that? 如何排除呢?

$(document).keypress(function(e) {
    if(e.which == 32) {
        alert('trigger');
    }
});

You can use nodeName to catch the event source: http://jsfiddle.net/t8jqb2rq/ 您可以使用nodeName捕获事件源: http : //jsfiddle.net/t8jqb2rq/

//Array of sources you want to include
var includeIn = ['BODY','TEXTAREA'];
$(document).keypress(function(e) {
    if(e.which == 32 && includeIn.indexOf(e.target.nodeName) != -1) {
        alert('trigger');
    }

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM