如何使用jQuery绑定多个事件处理程序到一个事件? [英] How can I bind many event handlers to one event using jQuery?

查看:95
本文介绍了如何使用jQuery绑定多个事件处理程序到一个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将如何绑定两个不同的事件处理程序,以将其绑定到一个事件处理程序同样的事件?

  $(this).focus(function(){/*...*/}); 
$(this).focus(function(){/*...*/}); //替换前一个?

我缺少什么?



更新



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






结语



该问题与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); //不工作
});


解决方案

我只用这个代码检查一下:

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

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

当输入字段集中时,它会产生两个控制台消息。



您的处理程序没有运行吗?


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

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


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.

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
});

解决方案

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?

这篇关于如何使用jQuery绑定多个事件处理程序到一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆