简体   繁体   English

在模糊事件之后如何获取焦点事件?

[英]How to get the focus event following the blur event?

I like to know what the onFocus event is (if there will be any) when I receive an onBlur event in my (javascript) handler. 当我在(javascript)处理程序中收到onBlur事件时,我想知道onFocus事件是什么(如果会有)。 How is this possible? 这怎么可能?

Example: I have a group of textboxes that form one group. 示例:我有一组文本框,它们组成一个组。 Only if the whole group loses focus I want to inform subscribers about the group onBlur event. 仅当整个组失去焦点时,我才想通知订户有关组onBlur事件的信息。 So if an onBlur event occurs I only want to forward it when I notice that it's not followed by an onFocus event on another textbox in the group.. 因此,如果发生onBlur事件,则仅在发现该组的另一个文本框上没有紧随其后的onFocus事件时才转发它。

I do this now through a delay and looking if a focus event occurred in the meantime. 我现在通过延迟来执行此操作,并查看在此期间是否发生了焦点事件。 But I don't like this solution so much as: because of the delay the blur occurs after the focus and the event that it contained in the delayed blur is "old" which can be tricky in further processing.. 但是我不喜欢这种解决方案,因为:由于延迟,模糊发生在焦点之后,并且延迟模糊中包含的事件是“旧的”,这在后续处理中可能会比较棘手。

My idea was: looking in the "event queue", when I receive the blur event, to detect any onFocus event that will follow... But I have no idea if this is possible and how to do this :(... 我的想法是:当我接收到模糊事件时,在“事件队列”中查找以检测将要发生的任何onFocus事件...但是我不知道这是否可行以及如何执行:(...

-- Edit: -编辑:

Having reviewed your question I'm not quite sure what you're asking, so I'm not sure if what I've posted is suitable. 审核完您的问题后,我不太确定您要问什么,因此不确定所发布的内容是否合适。 Regardless, I leave it here for consideration. 无论如何,我把它留在这里考虑。

You can do it quite trivially with a map and the 'this' variable: 您可以使用地图和'this'变量轻松完成此操作:

var map = new Object();

...

function doBlur (obj) {
   if(map[obj] == "focus"){
   }
}

function doFocus (obj) {
    map[obj] = "focus";
}

<input onblur="doBlur(this);" onfocus="doFocus(this);" />

But even further, given that only one textbox can be focused at a time, you can just have a variable named lastFocus , and check that that object equals the one you want. 但更进一步,鉴于一次只能集中一个文本框,您可以只拥有一个名为lastFocus的变量,并检查该对象是否等于您想要的对象。

The focus and the blur event should be added to the event queue, so a simple 1-100 ms setTimeout would probably do the trick. 应该将焦点和模糊事件添加到事件队列中,因此简单的1-100毫秒setTimeout可能会解决问题。

I know you aren't using jQuery - but its easier for me to air code an example using it: 我知道您没有使用jQuery-但对我来说,使用它编写代码示例更容易:

var blurTimeout;

function blurAlert() {
   alert('Lost Focus');
}

// assign these two simple functions to all inputs on the page.
$('input').blur(function() {
  blurTimeout=setTimeout(blurAlert, 1);
}).focus(function() {
  clearTimeout(blurTimeout);
});

I just tested it out at 1ms in firefox, I'm pretty sure that timing will hold well for other browsers as well. 我刚刚在firefox中以1ms的速度对其进行了测试,我很确定计时对于其他浏览器也同样适用。 Because of the way events are handled, blur fires, then focus, then the timer. 因为处理事件的方式不同,所以先进行模糊触发,然后进行聚焦,然后再进行计时器。

If you really want to make sure to keep the this around from the event your event handler turns into: 如果您确实要确保在事件发生时避免this ,那么事件处理程序将变为:

 function() { 
   blurTimeout = setTimeout(function() {
     blurAlert.apply(this,arguments);
   }, 1);
 }

You can use delegation method for blur and focus 您可以使用委派方法进行模糊和聚焦
So you will check what was focused. 因此,您将检查重点。

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

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