How to exclude (.not) selector with Jquery (with live function)
I have an live event on all link or multiple selector on the page and i have multiple selector i don't want active the event. Normally, when the first live function is executed on all link, the e.preventDefault() is activate. But if one of the selector is in my exclude list, i want execute the link normally. I have tried this code :
$(selectorListA).live('click',function(e) {
// stop all event
});
$(selectorListB).live('click',function(e) {
e.preventDefault();
// do action
});
I have tried this to :
$(selectorListA).not(selectorListB).live('click',function(e) {
e.preventDefault();
// do action
});
All of this solutions don't work, the only way i have is this code, it work but in chrome an error is showed before the click event is execute, hmmm i'm not sure is good :
$(selectorListB).live('click',function(e) {
die();
});
$(selectorListA).live('click',function(e) {
e.preventDefault();
// do action
});
I don't know what i can开发者_StackOverflow take for execute all selectorListA with exclude all selectorListB. Can you help me please, thank you!
What is the error you get in Chrome? Seems that the problem is not with your selectors.
Either way, something like this should work:
$(selectorListA).live('click',function(e) {
if ($(this).is(selectorListB)) }{
// stuff for b group only
}
// stuff for both groups
});
The version using .not()
won't work because live
attaches a document-level handler and uses the original selector to test whether the event should apply. So actually selecting and filtering before calling live won't work as it would with click()
.
精彩评论