开发者

Does binding a click event to a button that already has a click event bound cause issues?

Does binding a click event to a button that already h开发者_如何学Cas a click event bound cause issues?


No, the events accumulate.


No. You will now have two click events on that button. Both will happen in order they were bound.

Try out this simple example: http://jsfiddle.net/FQvJq/

$('#test')
    .click(function(){ alert('click callback 1')})
    .click(function(){ alert('click callback 2')})


Technically no, but it depends more on what your events are doing. They will execute in sequence. Id think more about what impact the two events could have on one another.

Its also perhaps worth binding defined methods to the events as this will make for easier unbinding - even if it doesn't apply to you its still better design, in my opinion.


As of jQuery 1.4.2 duplicate event handlers can be bound to an element instead of being discarded. For example:

function test(){ alert("Hello"); }
$("button").click( test );
$("button").click( test );

The above will generate two alerts when the button is clicked.

In jQuery 1.4.3 you can now pass in 'false' in place of an event handler. This will bind an event handler that's equivalent to: function(){ return false; }. This function can be removed at a later time by calling: .unbind( eventName, false ).

I think this might be helpful you.Thanks

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜