How to access JQuery Plugin Extended Methods
I'm currently working with a JQuery Plugin called Raty (among many others), as normal the plugin does the following:
(function($){
$.fn.raty = function(settings, url){
// Some default operations
// Some functions
this.activate = function(){}
this.reset = function(){}
};
});
Then in my code, I call it by doing so:
$('#someDivId').raty();
The problem occurs, when I want to call one of these extended methods (e.g. reset()) on the "someDivId", I look for it at a later stage开发者_StackOverflow中文版 with jquery $('#someDivId') but when I execute any of the extended methods on it, an error occurs (the object seems to not have these methods attached to it).
This is the piece of code which is not working:
$('#someDivId').activate();
My question is, do you know if it is possible at all to access these methods from outside the plugin? And if not, do you know a generic way of doing this?
In the current example I ended up adding a hidden link which can be clicked to trigger any of these methods. (But it still an ugly hack for me :P)
Thanks, Nicolas
$('#someDivId').raty();
will attach the functions to this particular jQuery object instance. When you call $('#someDivId')
again, a new instance will be created.
You have to keep a reference to the original instance:
var $div = $('#someDivId').raty();
// later
$div.activate();
This assumes that raty()
is a good citizen and returns this
. If not, you have to make the reference first:
var $div = $('#someDivId');
$div.raty();
精彩评论