最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery multiple event handlers - Stack Overflow

programmeradmin4浏览0评论

I have defined event handlers according to their classnames in the latest project that I'm working on.

for ex. all element with the class name "foo" should respond in a particular way on change event. and all elements with the class name "bar" should respond in some other way.

Now some of my elements fall under both classes i.e. class="foo bar" and they should respond in both ways. Right now, only one of the event handler function is being called.

How can I make both the responses execute simultaneously.

I have defined event handlers according to their classnames in the latest project that I'm working on.

for ex. all element with the class name "foo" should respond in a particular way on change event. and all elements with the class name "bar" should respond in some other way.

Now some of my elements fall under both classes i.e. class="foo bar" and they should respond in both ways. Right now, only one of the event handler function is being called.

How can I make both the responses execute simultaneously.

Share Improve this question asked Oct 2, 2012 at 5:10 aayush shresthaaayush shrestha 1,9182 gold badges17 silver badges33 bronze badges 2
  • 1 Short answer, you can't. One will execute before the other. Simultaneous isn't possible, but, both should be executing in the order in which they were raised. Can you give us some code to look at? – The Internet Commented Oct 2, 2012 at 5:16
  • you can not execute both events "simultaneously". by the way what are these events, somehow it also depends on the events – xkeshav Commented Oct 2, 2012 at 5:31
Add a comment  | 

2 Answers 2

Reset to default 14

It depends on how you are binding the events. If you are binding them via jQuery and not overwriting the handlers via x.onchange = function() { ... } - all the bound event handlers will get executed. This is because jQuery queues the event handlers instead of overwriting the previous binding(s).

Check this fiddle to see multiple events getting fired:
and

Check this fiddle to see how the event handlers are overwritten causing only the last bound handler to fire

What you want is a variation of "behaviors" pattern.

It lets you to automatically process events on elements with given classes or other attributes.

The usual implementation is to listen to events on "document", and then, by event.target, define the action.

For example: fiddle(http://jsfiddle.net/PRkAr/)

$(document).on('click change', '.foo.bar', function(e) {
  var classes = this.className.split(/\s+/);
  $.each(classes, function(i, cls) {
    handle(e, this, cls);
  })
})

function handle(e, elem, cls) { 
  // example: Event type click on elem DIV with class foo
  alert("Event type " + e.type + " on elem " + elem.tagName + " with class " + cls);
} 

Here function handle with process all events on elements with classes of your choice. If you wish to add other events or classes, just append them into the "on" list.

发布评论

评论列表(0)

  1. 暂无评论