admin管理员组

文章数量:1399269

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

本文标签: javascriptOnblurwhat is the next control which will gain focusStack Overflow