最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Onblur, what is the next control which will gain focus? - Stack Overflow

programmeradmin2浏览0评论

I am firing some code onblur() of a textbox. But I want to do something different if the user clicked on the cancel button to cause that onblur(). Is there a way within the onblur() event handler function to know what the control is that is about to receive focus and react acordingly?

-Joseph

I am firing some code onblur() of a textbox. But I want to do something different if the user clicked on the cancel button to cause that onblur(). Is there a way within the onblur() event handler function to know what the control is that is about to receive focus and react acordingly?

-Joseph

Share Improve this question edited Jan 30, 2010 at 1:26 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Jan 29, 2010 at 23:43 BrokeMyLegBikingBrokeMyLegBiking 5,98815 gold badges52 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Yes, you can get the target like this:

var target = event.explicitOriginalTarget || document.activeElement;

(I think I found this on StackOverflow somewhere).

I don't think there is a built in way, but you can hack together something by attaching focus events to all the form elements and setting some globally accessible variable about who received focus.

Something like this (you may want to fine-tune the time values):

var focusedElem = null;
var focusedTime = 0;

$("input", "#myForm").focus(function() {
    focusedElem = this;
    focusedTime = new Date();
})
.blur(function() {
    setTimeout(function() { //set a small timeout to wait for the other control to receive focus and set itself as focusedElem
        if (focusedElem && (new Date() - focusedTime) < 15) {
            //do Something with focusedElem;
        }
    }, 10);
});
发布评论

评论列表(0)

  1. 暂无评论