简体   繁体   中英

Onblur, what is the next control which will gain focus?

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

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);
});

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