Unbind a jQuery Plugin?
I have a plugin like
$.fn.mycoolplugin
which binds mousemove
to the document like
$(document).("mousemove", function() {
//bunch of stuff
});
after you call the function on a selector
$('开发者_开发知识库.myclass').mycoolplugin();
How would I unbind this because the mousemove
is bound to the document and OTHER stuff in my code uses mouseenter
and mouseleave
?
You can unbind any handler by calling .unbind
:
$(document).unbind("mousemove");
This will unbind all mousemove
handlers from document
. Do note that doing this might break the plugin's functionality (with the risk of breaking any other code/plugin that adds mousemove
handler to document
).
You would change how your plugin binds and then releases it's attached handlers like so:
- You make a function that is responsible for binding all your initial events (such as the mousemover, etc)
- You make a function that is responsible for unbinding all your initial events.
When you bind to an event, do not use an anoymous function. Instead, define a specific handler for the event. That way, when you attach, you do so like so:
$(document).bind('mousemove', myFuncDelegate);
And then when you need to remove it from scope, you unbind:
$(document).unbind('mousemove', myFuncDelegate);
That way you only unattach your events. See http://api.jquery.com/unbind/ for details.
Simply call your bind method on load, and when you decide to unload it, call the unbind method.
精彩评论