Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery need alternative to focusout()

Given the sample markup:

<div>
    <input />
    <input />
    <input />
</div>

How can one, via jQuery, determine that div has lost focus?

I can use focusout() but that's not quite what I need. With focusout, it will get triggered as one tabs from input to input, as it's actually detecting (via event bubbling) that the input is losing focus.

Another way to word the requirement: I need to know when focus has moved OUTSIDE of the div.

I asked a similar question before:

jquery focusin() and preventing bubbling

But that was related to a pop-up UI and one can get around that by inserting a blank DIV behind it and putting a click/focus event on that as a trigger but that's won't work for this situation.

The next thought I had was to test for focusin when calling focusout:

    $(".myobject").focusout(function(element) {
    if(!($(this).focusin())){
        console.log('doYourThing ' + $(this));
    }
});

Alas, that doesn't work (I'm guessing due to the fact that it's evaluating focusin during the focusout event and, as such, it hasn't detected focusin yet.

Any clever solutions to this problem? Am I maybe missing a native jQuery event that does exactly what I'm looking for?

UPDATE:

Actually, the simplified question:

I need the equivalent of $('div').blur() but that would actually work on a div (since blur can't be triggered from a div)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Taking Pointy's answer and going a little further with it.

creating a (simple) focuslost event plugin

(function($) {
    // will store the last focus chain
    var currentFocusChain = $();
    // stores a reference to any DOM objects we want to watch focus for
    var focusWatch = [];

    function checkFocus() {
        var newFocusChain = $(":focus").parents().andSelf();
        // elements in the old focus chain that aren't in the new focus chain...
        var lostFocus = currentFocusChain.not(newFocusChain.get());
        lostFocus.each(function() {
            if ($.inArray(this, focusWatch) != -1) {
                $(this).trigger('focuslost');
            }
        });
        currentFocusChain = newFocusChain;
    }
    // bind to the focus/blur event on all elements:
    $("*").live('focus blur', function(e) { 
        // wait until the next free loop to process focus change
        // when 'blur' is fired, focus will be unset
        setTimeout(checkFocus, 0);
    });

    $.fn.focuslost = function(fn) {
        return this.each(function() {
            // tell the live handler we are watching this event
            if ($.inArray(this, focusWatch) == -1) focusWatch.push(this);
            $(this).bind('focuslost', fn);
        });
    };
})(jQuery);

Example Use

$("div").focuslost(function() {
  $(this).append("<div>Lost Focus!</div>");
});

jsfiddle demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...