I've attached some live() listeners in order to automatically make ajax calls for every link with ajax in the url:
$("document").ready(function() {
$('a[href^="/ajax"]').live('click', call);
});
function call(e, context, link) {
e && e.preventDefault();
link = link || this;
if(typeof link == "string" || !$(link).hasClass("disabled")) {
newObj(SPZ.AjaxCall, link, context);
}
};
But sometimes I want to override this (so that I can specify a context in which to find callback functions), so I wrote a jQuery method to do this
$.fn.customAjax = function(context) {
return this.die().click(function(e){
call.call(this, e, context);
});
};
$(".save").customAjax(myObj);
The problem I have开发者_StackOverflow is that if the live handler is added to a collection of elements I can't just use die() on one of those elements; die() only works, it seems, if you apply it to all the elements live() was used on.
Can anyone suggest a workaround that will suppress the live() event? And, as an aside, what are people's opinions on suggesting the jQuery team change the behaviour of die() so that it can be used to remove live() handlers on individual elements; are there any reasons why this would be a bad idea?
Where does call
come from? I do not see it defined somewhere in this code section.
Besides that, i think your this
does not refer to a jQuery object, but a DOM node. Try wrapping it:
$.fn.customAjax = function(context) {
return $(this).die().click(function(e){
call.call(this, e, context);
});
};
$(".save").customAjax(myObj);
A bit more playing around and I found that adding a simple return false at the end of my jQuery method did the trick. It'll cause problems if I ever want to attach other listeners for the same click, but it'll do for now.
精彩评论