开发者

extend click event for plugins?

is it possible to extend normal events for plugins in a general way?

e.g. say i have a plugin A which does stuff and react to the click event on an element i would now to perform some actions when that element is clicked, and AFTER i have done my stuff i want the plugin to do whatever it does onlick.

at the moment i am manually looking at the plugin and create开发者_如何学C custom code, but maybe there is some general way that one can prevent a click event for a plugin temporarily, and then trigger it?


You can bind multiple event handlers to a single event, so, for example, you could do something like this:

$("#theElement").bind("click", yourFunction);

This would be executed as well as the existing code when the click event is triggered. However, there's nothing to say it will run before the existing code.

Unfortunately, jQuery does not provide a method for sorting the events, so I think your best bet will probably be to simply modify the click event handler in the plugin to call your function first.


The click can be prevented by adding:

return false;

Which basically does the same as:

e.preventDefault(); // prevents default behaviour when clicking on it
e.stopPropagation(); // prevent bubbling up the dom tree

If those aren't in the code the click should just do what it normally does.

I have been in a situation where I had to do return true; to force it.

But I don't know exactly when that was needed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜