简体   繁体   English

Javascript 事件处理程序在主体上但不在输入上

[英]Javascript event handler on body but not on input

I have the following event handler我有以下事件处理程序

document.addEventListener('keydown', handleBodyKeyDown, false);

HOW DO i prevent it from occurring when inside a input box我如何防止它在输入框内时发生

Within your handleBodyKeyDown function, check if handleBodyKeyDown函数中,检查是否

 event.target.tagName.toUpperCase() == 'INPUT'

(or 'TEXTAREA' ). (或'TEXTAREA' )。

Note: For older versions of IE, use event.srcElement.tagName . 注意:对于旧版本的IE,请使用event.srcElement.tagName

Like so: 像这样:

document.addEventListener('keydown', handleBodyKeyDown, false);

function handleBodyKeyDown(event)
{
    var e = event || window.event,
        target = e.target || e.srcElement;

    if (target.tagName.toUpperCase() == 'INPUT') return;

    // Now continue with your function
}

PS Why are you using addEventListener if you have jQuery on the page? PS如果页面上有jQuery,为什么要使用addEventListener In jQuery, all of this gets sorted out for you: 在jQuery中,所有这些都为你解决了:

$(document).on('keydown', ':not(input)', function(e)
{
    // Your code goes here...
});

This worked for me:这对我有用:

const fromInput = event => event.srcElement instanceof HTMLInputElement;

function handleBodyKeyDown(event) {
  if(fromInput(event))
    return;
  
  // do your magic here
}

In your handleBodyKeyDown method, check to see if the event originated on an input element: handleBodyKeyDown方法中,检查事件是否源自input元素:

function handleBodyKeyDown(event) {
    if (event.target.tagName.toUpperCase() === 'INPUT') {
        return; // do nothing
    }

    // do the rest of your code
}

Note that the toUpperCase call is necessary because the conditions that determine the case of the tagName property are quite complicated and sometimes all but uncontrollable. 请注意, toUpperCase调用是必需的,因为确定tagName属性大小写的条件非常复杂,有时几乎无法控制。

See event.target at MDN . 请参阅MDN上的event.target

If you are using jQuery you can try this which uses is() method to test the target element is input then do nothing. 如果您正在使用jQuery您可以尝试使用is()方法来测试目标元素是否input然后什么都不做。

function handleBodyKeyDown(event) {
    if ($(event.target).is("input")) {
        return; 
    }
    else{
       //Do your stuff here
    }
}

You could do something like: 你可以这样做:

handleBodyKeyDown = function(e) {
   var e = e || window.event
   if (e.target.tagName != "INPUT") {
       // handle this since it isn't input
   }
}

Sometimes (as to me) it is better not to prevent it to occur, but to ignore in the event cases, when it occured in the input. 有时(对我而言)最好不要阻止它发生,而是在事件情况下,当它在输入中发生时忽略。 It's looks like this is also your case as well. 看起来这也是你的情况。

Just inspect evt.target || evt.srcElement 只需检查evt.target || evt.srcElement evt.target || evt.srcElement property (modern frameworks do this normalization work for you, so, most probably this will be called target) whether it's input or not. evt.target || evt.srcElement属性(现代框架为您执行此规范化工作,因此,很可能这将被称为目标)无论是否输入。 If not, just ignore. 如果没有,请忽略。

QuirksMode tells you how to get an event's target. QuirksMode告诉您如何获取事件的目标。 You can check that it is not an input: 您可以检查它不是输入:

function doSomething(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    if( targ.tagName != "INPUT" ) {
       //Perform your action here
    }
}

Your question is tagged jQuery, in which case you can just test event.target as the framework normalizes this for you. 你的问题被标记为jQuery,在这种情况下你可以测试event.target,因为框架会为你规范化。

$(document).bind("keydown", function (event) {
    if(event.target.tagName != "INPUT") {
        //Do something
    }
});

HandleBodyKeyDown function will be invoked in any case. 在任何情况下都会调用HandleBodyKeyDown函数。 You can not prevent its call on the method of recording as you indicated. 如您所示,您无法阻止其对录制方法的调用。 You can only add a logic for checking if this an 'input' and return. 您只能添加一个逻辑来检查这是否为“输入”并返回。 Additionaly (if needed) you can prevent it from bubble up: 另外(如果需要)你可以防止它冒泡:

function handleBodyKeyDown(ev) {
  ev=ev||event;
  var sender=ev.target||ev.srcElement;
  if(sender.tagName.toLowerCase()==="input") {
    if(ev.stopPropagation)ev.stopPropagation();
    else ev.cancelBubble=true; // for IE8 or less
    return true; // do not prevent event from default action
  }
  // your code for global keydown
}

如果你正在使用Prototype(你已标记但你还有两个其他框架被标记),那么可以注册和过滤事件,如下所示:

document.on('keydown', ':not(input)', handleBodyKeyDown);

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

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