jQuery:live('focus') 和 live('blur') 嵌套在命名空间中,在 1.3.2 中不起作用

发布于 2024-10-06 16:17:11 字数 1366 浏览 0 评论 0原文

我有以下 html,

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

我希望工具提示在输入获得焦点时淡入,因此我使用正确的方法创建了一个命名空间(它包含以下方式用于输入/表单的所有 javascript),

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

})();

然后调用它当文档加载时

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

并且... 它不适用于 jQuery 1.3.2它适用于 1.4.2,但我真的准备好推动库更改了那一刻。

知道我如何才能拥有我的蛋糕(命名空间!)并吃掉它(实时工作!)?


没有人会拒绝让自己出丑,这是我跳过的明确摘录,直接来自马嘴:

在 jQuery 1.3.x 中仅以下 JavaScript 事件(在 除了自定义事件)可以是 与 .live() 绑定:click、dblclick、 按键、按键、按键、鼠标按下、 鼠标移动、鼠标移开、鼠标悬停和 鼠标松开。

(...)

从 jQuery 1.4.1 开始甚至可以使用实时焦点和模糊(映射 更合适的是,冒泡, 事件 focusin 和 focusout)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-10-13 16:17:11

事实上,如果没有一些严重的更改,这些更改可能会比升级到 1.4+ 带来更多问题。事件系统在 1.4.2 中进行了巨大的修改,并从那时起进行了进一步的改进...您的时间花在升级到 jQuery 1.4+ 上比尝试在 1.3.x 中提供对此的支持要好得多。

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.

暖树树初阳… 2024-10-13 16:17:11

你总是可以这样做......

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

})();

而不是使用 live。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文