Add function to existing JQuery plugin

只谈情不闲聊 提交于 2019-12-30 10:13:06

问题


Is it possible to add a function 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.


回答1:


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.




回答2:


Make sure you are including the plugin after jQuery.



来源:https://stackoverflow.com/questions/4658130/add-function-to-existing-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!