向现有的JQuery插件添加功能 [英] Add function to existing JQuery plugin

查看:216
本文介绍了向现有的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插件添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆