如何在 jQuery 中使用 .live 绑定多个事件?

发布于 2024-12-07 08:16:41 字数 623 浏览 0 评论 0原文

我有一个包含两个事件的输入文本,我动态地克隆了它几次。为了正确绑定此元素,我使用:

注意:此输入文本具有 focusin 和 focusout 事件。

$('[selector_input]').die().live( 'focusin', function() {
       alert( 'You focused me' );
});

$('[selector_input]').die().live( 'focusout', function() {
       alert( 'Bye!' );
});

最终结果是所有输入仅执行:focusout 事件。如果我不使用 .die(),我将重复调用相同的事件。

现在的解决方案可能是:

$('[selector_input]').die().live( 'focusout focusin', function(event) {
    if (event.type == "focusin")
       alert( 'You focused me' );
    else
       alert( 'Bye!' );
});

但我喜欢第一个代码的独立性。

I have an input text with two events and dinamically I clone it several times. To bind this elements correctly I use:

NOTE: This input text has the focusin and focusout events.

$('[selector_input]').die().live( 'focusin', function() {
       alert( 'You focused me' );
});

$('[selector_input]').die().live( 'focusout', function() {
       alert( 'Bye!' );
});

The final result is that all inputs only executes: focusout event. If I don't use .die(), I will get repeated call the same events.

Now the solution could be:

$('[selector_input]').die().live( 'focusout focusin', function(event) {
    if (event.type == "focusin")
       alert( 'You focused me' );
    else
       alert( 'Bye!' );
});

But I like and independence like the first code.

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

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

发布评论

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

评论(2

薯片软お妹 2024-12-14 08:16:41

只需指定调用 die() 的事件类型即可,但

$('[selector_input]').die('focusin').live( 'focusin', function() {
   alert( 'You focused me' );
});

$('[selector_input]').die('focusout').live( 'focusout', function() {
   alert( 'Bye!' );
});

我不太确定为什么需要 die() 调用,您是否重复绑定事件?

Just specify the event type for the call to die()

$('[selector_input]').die('focusin').live( 'focusin', function() {
   alert( 'You focused me' );
});

$('[selector_input]').die('focusout').live( 'focusout', function() {
   alert( 'Bye!' );
});

I'm not quite sure why you need the die() call though, are you binding the events repeatedly?

岁月静好 2024-12-14 08:16:41

您可以执行类似的操作

$('path to your input').live({
    focusin: function() { $('some output div').html('Focus!'); },
    focusout: function() { $('some output div').html('Bye!'); }
});

,或者您可以删除 .die() ——它的真正目的是删除该选择器以前的事件处理程序。

如果您正在使用警报框,我不会同时扰乱焦点事件 - 警报框出现时会成为活动的东西,导致聚焦的东西失去焦点(然后可能重新聚焦),并导致您的事件有时会无限地调用处理程序。

顺便说一句,请参阅 http://jsfiddle.net/9pjWa/ 了解如何使用 的示例。 live()——听起来你做错了。 .live()要点是,您不需要在每次添加元素时重新绑定事件;事件处理程序位于文档上,而不是元素上,并且与选择器匹配的每个元素都可以触发事件,无论它们是在.live()之前还是之后添加的。事实上,每次重新绑定时,您都会添加另一个事件处理程序来处理所有匹配元素,这也是您看到事件处理程序被多次调用的另一个原因。

You could do something like

$('path to your input').live({
    focusin: function() { $('some output div').html('Focus!'); },
    focusout: function() { $('some output div').html('Bye!'); }
});

or you could just remove the .die() -- its very purpose is to remove the previous event handlers for that selector.

If you're playing with alert boxes, i wouldn't mess with focus events at the same time -- the alert box becomes the active thingie when it appears, causing focused stuff to unfocus (and then possibly refocus), and causing your event handler to get called sometimes infinitely.

BTW, see http://jsfiddle.net/9pjWa/ for an example of how to use .live() -- it sounds like you're doing it wrong. The very point of .live() is that you do not need to re-bind the events every time you add an element; the event handler is on the document, not the elements, and each element that matches the selector can trigger the event whether they were added before or after .live(). In fact, every time you re-bind, you add another event handler that handles all of the matching elements, which is another reason why you're seeing the event handler called a bunch of times.

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