开发者

How to access JQuery Plugin Extended Methods

开发者 https://www.devze.com 2023-03-05 20:40 出处:网络
I\'m currently working with a JQuery Plugin called Raty (among many others), as normal the plugin does the following:

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号