Add function to existing JQuery plugin(向现有的 JQuery 插件添加功能)
问题描述
是否可以在不修改实际插件的情况下向插件添加功能?我可以在我网站的 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 插件添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向现有的 JQuery 插件添加功能


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01