如何使用jQuery将多个事件处理程序绑定到一个事件?

时间:2021-12-05 20:26:43

I'm experience strange behaviour with jQuery while trying to attach more than one event handler to a single event.

在尝试将多个事件处理程序附加到单个事件时,我遇到了jQuery的奇怪行为。

How would I bind two different event handlers, to the same event?

如何将两个不同的事件处理程序绑定到同一事件?

$(this).focus(function(){/*...*/});
$(this).focus(function(){/*...*/}); // replaces the previous one?

What am I missing?

我错过了什么?

Update

Do you know if it affects how event data is routed? It appears that adding a second event handler causes eventObject.data property to return undefined...?

您知道它是否会影响事件数据的路由方式吗?看来添加第二个事件处理程序会导致eventObject.data属性返回undefined ......?


Epilogue

The problem was somehow related to the way jQuery normalizes event handling and how the eventObject data property changed depending on routing, I had a delay timer at one point which read the property at a later time when it was undefined, I solved it by simply creating a local temporary for it.

这个问题在某种程度上与jQuery规范化事件处理的方式以及eventObject数据属性如何根据路由改变有关,我在一个点上有一个延迟计时器,它在以后读取属性时未定义,我通过简单创建解决了它一个当地临时的。

obj.inputText.bind('blur', obj, function(e) {
    var div = e.data.div;
    setTimeout(function() { div.hide(); }, 333); // works!
    // setTimeout(function() { e.data.div.hide(); }, 333); // does not work
});

3 个解决方案

#1


That does work. I just double checked with this code:

这确实有效。我只是仔细检查了这段代码:

$(document).ready(function() {
  $("#FirstName").focus(function() {
    console.log("focus1");
  });

  $("#FirstName").focus(function() {
    console.log("focus2");
  });
});

And it does produce two console messages when the input field is focused.

当输入字段被聚焦时,它会产生两个控制台消息。

Are you positive that both your handlers aren't running?

你是否同意你的处理程序都没有运行?

#2


Can't you create on "master" function that calls all needed handlers?

你不能在调用所有需要的处理程序的“主”功能上创建吗?

$(this).focus( function() { function1();function2();etc..... });

$(this).focus(function(){function1(); function2(); etc .....});

#3


This should theoretically work, try if all handlers are fired using:

理论上这应该有效,尝试使用以下方法解雇所有处理程序:

$(this).focus( function() { alert('handler1'); });
$(this).focus( function() { alert('handler2'); });
$(this).focus();

#1


That does work. I just double checked with this code:

这确实有效。我只是仔细检查了这段代码:

$(document).ready(function() {
  $("#FirstName").focus(function() {
    console.log("focus1");
  });

  $("#FirstName").focus(function() {
    console.log("focus2");
  });
});

And it does produce two console messages when the input field is focused.

当输入字段被聚焦时,它会产生两个控制台消息。

Are you positive that both your handlers aren't running?

你是否同意你的处理程序都没有运行?

#2


Can't you create on "master" function that calls all needed handlers?

你不能在调用所有需要的处理程序的“主”功能上创建吗?

$(this).focus( function() { function1();function2();etc..... });

$(this).focus(function(){function1(); function2(); etc .....});

#3


This should theoretically work, try if all handlers are fired using:

理论上这应该有效,尝试使用以下方法解雇所有处理程序:

$(this).focus( function() { alert('handler1'); });
$(this).focus( function() { alert('handler2'); });
$(this).focus();