Jquery define a function and call it as being a method of a selected object? [duplicate]
Possible Duplicate:
In jQuery, what does $.fn. mean?
Can someone explain the code below a little more ?
I see that a function called isdirty is defined. What I don't get is:
- What is $.fn ?
The second line calls the function on a selector. The function now seems a method of the object I selected ??? Can i attach this function to all objects like this?
(function($) { $.fn.isdirty = function(settings) { alert(test); }}; 开发者_JS百科 $('.dirtycheck').isdirty();
$.fn
is a shortcut tojQuery.prototype
. When you augment theprototype
, all jQuery objects will have access to that new method.- Any plugin created like this can access the selected set with
this
. The general form is...
$.fn.abc = function() {
// Return it so we can keep chaining.
return this.each(function() {
// Do whatever.
});
};
$.fn
=== $.prototype
It's the prototype of jQuery. If you extend it you can use it as a method on a jQuery object.
Any object created with the jQuery method (i.e. $("#foo")) will inherit all methods defined on $.fn
So this means you can call $("#foo").isDirty();
精彩评论