是否可以在不修改实际插件的情况下向插件添加功能?我可以在我网站的 js 文件中做这样的事情吗?
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();
}
或
(function($){
$.fn.Watermark.Refresh = function() {
$.Watermark.HideAll();
$.Watermark.ShowAll();
};
})(jQuery);
都不行,第一个说 $ 未定义,第二个说 jQuery 未定义...
neither worked, the first says $ is undefined, the second that jQuery is undefined...
想法?
解决方案:任何一种方法都可以,只需在站点 js 文件之前包含 jquery 文件即可.
Solution: Either method works, just include the jquery file before the site js file.
如果你愿意,你可以添加这些函数,但你必须确保你也加载了 jQuery 本身和要修改的插件.如果您遇到这些错误(未定义 jQuery 或$"),那么您没有正确地做到这一点.
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"; };
那么就可以调用它了:
var str = $.fn.css.myFunction();
但那又怎样?这对我有什么好处?我觉得用处不大.
but so what? What good does that do me? I don't think it's very useful.
这篇关于向现有的 JQuery 插件添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!