問(wèn)題描述
是否可以在不修改實(shí)際插件的情況下向插件添加功能?我可以在我網(wǎng)站的 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);
都不行,第一個(gè)說(shuō) $ 未定義,第二個(gè)說(shuō) jQuery 未定義...
neither worked, the first says $ is undefined, the second that jQuery is undefined...
想法?
解決方案:任何一種方法都可以,只需在站點(diǎn) js 文件之前包含 jquery 文件即可.
Solution: Either method works, just include the jquery file before the site js file.
推薦答案
如果你愿意,你可以添加這些函數(shù),但你必須確保你也加載了 jQuery 本身和要修改的插件.如果您遇到這些錯(cuò)誤(未定義 jQuery 或$"),那么您沒(méi)有正確地做到這一點(diǎn).
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.
現(xiàn)在,雖然您確實(shí)可以添加這些功能,但我想知道重點(diǎn)是什么.如果我要這樣做,例如:
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"; };
那么就可以調(diào)用它了:
var str = $.fn.css.myFunction();
但那又怎樣?這對(duì)我有什么好處?我覺(jué)得用處不大.
but so what? What good does that do me? I don't think it's very useful.
這篇關(guān)于向現(xiàn)有的 JQuery 插件添加功能的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!