覆盖jQuery插件方法,默认情况下且针对单个实例 [英] Overriding jQuery plugin methods, as default and for single instances
问题描述
基本问题是:如何在不编辑原始插件文件的情况下执行插件方法的基本覆盖?
The basic question is: How do I perform a basic override of a plugin method without editing the original plugin file?
是否可以为特定实例创建替代:
Is it possible to create an override for a specific instance:
示例: rtf插件使用:
Example: An rtf plugin uses:
$('selector').wysiwyg('setContent',newContent);
为了将rtf文本显示为只读,我希望将相同的方法应用于div而不是IFRAME的正文
In order to display the rtf text as readonly I would like for the same method to be applied to a div instead of the body of the IFRAME
我想用我自己的代码覆盖原始的'setContent',仅用于此元素.
I would like to overwrite the original 'setContent' with my own code, just for this one element.
谢谢
推荐答案
(function($){
var _oldcss = $.fn.css;
$.fn.css = function(prop,value){
if (value.toLowerCase() === 'somecolor') {
return _oldcss.call(this,prop,'#EA7E5D');
} else {
return _oldcss.apply(this,arguments);
}
};
})(jQuery);
您可以通过这种方式轻松地覆盖(我喜欢称其为半挂钩)功能.
在此示例中,您覆盖了.css()
jQuery函数并返回一个自定义值
的值等于"somecolor",否则,原始函数将被调用
传递的参数.
You can easily overwrite (I like to call it semi-hooking) functions in that manner.
In this example, you overwrite the .css()
jQuery function and return a custom value
of your value equals 'somecolor', otherwise, the original function is called with
passed arguments.
这篇关于覆盖jQuery插件方法,默认情况下且针对单个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!