开发者

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

开发者 https://www.devze.com 2023-01-26 19:46 出处:网络
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 n

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 focu开发者_运维知识库sed 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消