开发者

Add function to existing JQuery plugin

Is it possible to add a func开发者_JAVA技巧tion to a plugin without modifying the actual plugin? Can I do something like this in my site's js file?

$.fn.Watermark.Refresh = function() {
        $.Watermark.HideAll();
        $.Watermark.ShowAll();
    }

or

(function($){
$.fn.Watermark.Refresh = function() {
        $.Watermark.HideAll();
        $.Watermark.ShowAll();
    };
})(jQuery);

neither worked, the first says $ is undefined, the second that jQuery is undefined...

ideas?

Solution: Either method works, just include the jquery file before the site js file.


You can add those functions if you want to, but you'll have to make sure that you're also loading jQuery itself and the plugin to be modified. If you're getting those errors (that jQuery or "$" are not defined), then you have not correctly done that.

Now, though it's true that you can add those functions, I have to wonder what the point would be. If I were to do this, for example:

$.fn.css.myFunction = function() { return "hello world"; };

then it would be possible to call it:

var str = $.fn.css.myFunction();

but so what? What good does that do me? I don't think it's very useful.


Make sure you are including the plugin after jQuery.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜