开发者

Jquery define a function and call it as being a method of a selected object? [duplicate]

This question already has answers here: Closed 11 years ago.

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:

  1. What is $.fn ?
  2. 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();
    


  1. $.fn is a shortcut to jQuery.prototype. When you augment the prototype, all jQuery objects will have access to that new method.
  2. 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();

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜