简体   繁体   中英

How to determine which element gets focus in blur()?

Is it possible to find the element that currently has the focus in a cross-browser way?

In IE, it is possible to do $(x).blur(function(evt) { alert('Focus goes to ' + evt.toElement.id); }); and I need to do something similar in other browsers.

As an alternative, if its possible, I could do a setTimeout and then investigate the currently focused element, but I don't know to do that either.

You could just remember what was focused in a variable. Variable scope in JS works cross-browser :)

(function($){

var focused;
$(commonParentForElements).on('focus','input',function(e){
  focused = e.target;
}).on('blur','input',function(e){
  //do stuff with focused
  //when done, clean up
  focused = null;
})

})(jQuery);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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