简体   繁体   中英

jQuery: live('focus') and live('blur') nested in a namespace not working in 1.3.2

I've got the following html

<input class="hasToolTip" type="text" value="meh"/>
<span class="tooltip" style="display: none;">tooltip here</span>

i'd like the tooltip to fade in when the input gets focus, so i created a namespace (it contains all my javascript for inputs/forms in the following way) with the correct method

var inputCommon = (function() {
    return {
        SetupToolTips: function() {
            $(".hasToolTip").live('focus', function() {
                $(this).next(".tooltip").fadeIn();
            });
            $(".hasToolTip").live('blur', function() {
                $(this).next(".tooltip").fadeOut();
            });
        }
    };

})();

I then call it when the document is loaded

$(function() {
    inputCommon.SetupToolTips();
});

And... it doesn't work with jQuery 1.3.2 . It works with 1.4.2 though, but i'm really ready to push a library change at the moment.

Any idea how i can have my cake (namespace!) and eat it too (live working!)?


Not one to resist making a fool of myself, here is the explicit excerpt i had skipped, straight from the horse's mouth :

In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live(): click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

(...)

As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout).

You can't really, not without some serious changes that would likely introduce more issues than upgrading to 1.4+ would. The event system got a huge overhaul in 1.4.2, and further refinements since then...your time would be much better spent on upgrading to jQuery 1.4+ than trying to work support for this into 1.3.x.

You can always do it this way...

var inputCommon = (function() {
    return {
        SetupToolTips: function() {
            $(".hasToolTip").focus(function() {
                $(this).next(".tooltip").fadeIn();
            });
            $(".hasToolTip").blur(function() {
                $(this).next(".tooltip").fadeOut();
            });
        }
    };

})();

rather than using live.

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