开发者

Own jQuery plugin, scope problem

I want to make a plugin, which is ment to work with plain objects - I can iterate using each on. Thus i want to be accessible through jQuery's global object like $.myPluginFn().

(function($) { $.fn.test = function() { return 1; } })(jQuery)

Executing $j.test() gives an error:

TypeError: Object function (a,b){return new d.fn.init(a,b,开发者_StackOverflow社区g)} has no method 'test'

What am I doing wrong? Assigning to $.fn.myPluginFn works on the other hand.


If I understand you correctly and you want jQuery.test() to work you should be using:

$.test = ...

instead of

$.fn.test = ...

The latter is what you would use if you wanted jQuery(selector).test() to work.


the .fn.foo function gives a foo function to each jQuery Object. So if you create one you can call it like this: $("#id_of_item").foo();

you this is useful if yo want to manipulate items.

but if you want to create other functions you dont need to put into the jquery object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜