开发者

jQuery: Executing multiple actions on the same event on the same element but at two different points in time

We know that multiple calls to $(document).ready(); in the same page is possible. I need something similar for click events.

Situation: Another team implemented this somewhere in an external JS file:

$('#element').live('click', function() { /*  Do this */} ); 

It's a live() click listener on #element. I cannot modify this code or that file, but I need to take an additional action on the same element on the same event. So, two actions will occur when #element is clicked. In other words, I need this equivalent:

$('#element').live('click', function() { /*  Do this */} ); 
/* Some code later... */
$('#element').live('click', function() { /*  Add another action */} ); 

I tried doing the above, but the second call does not fire. It needs to be .live() since #element is being dynamically added.

Is there any way to add more actions to the same event on #element? Thank开发者_运维知识库s.


They are returning false, and that's stopping your event from being fired. See http://api.jquery.com/bind/

Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.


I would suggest that it may be easier to actually unbind the existing live() call and then include it in your own method along with your own code. That's assuming you're allowed to and it's practical to do so.


each time you bind an event handler you install it on a chain of handlers, all of which will get called. So its okay to bind yourself in addition to the code you cant change

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜