jQuery how to set a Binding Order
is there a way to set a binding over, precedence?
I have a link like so:
<a href="" class="popItUp" data-track"stuff"> </a>
popItUp binds to take the url and open in a popup.
data-track is another binding that is used to send event tracking data.
Problem: popItUp returns false, and the data-track binding is never fired. Is there a way to always make data-t开发者_运维技巧rack run first by jQuery as it will always return true?
Thanks
Code
// Mixpanel Click Tracking
$('[data-track]').live('click', function () {
console.log('track it please');
});
$('.popItUp').popupWindow({height:300,width:600,centerBrowser:1});
popUpWindow is a plugin: http://swip.codylindley.com/popupWindowDemo.html
Instead of returning false, why not simple prevent the default action?
$('.popItUp').click(function(e)
{
e.preventDefault();
// Do Stuff
});
That way, the event just isnt canceled, it just doesnt perform the default action.
If popItUp
handler is bound first and if it returns false
then data-track binding
will not be called. You need to bind data-track binding
first if at all you want to give it more precedence and not matter what popItUp
does.
Looking at your edited question, data-track binding
is done using live
which works on event bubbling mechanism. If popItUp
handler returns false
then it is not going to work. May be you can try to prevent default behavior using e.preventDefault();
of anchor instead of returning false inside the popItUp
handler.
live
works by catching events that bubble up to the <body>
tag.
Therefore, any handlers added directly to an element will run before live
handlers.
Calling stopPropagation()
(or return false
) inside direct handlers will prevent it from bubbling, so live
won't catch it.
精彩评论