Overriding jQuery plugin methods, as default and for single instances

走远了吗. 提交于 2019-12-10 06:19:52

问题


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:

Example: An rtf plugin uses:

$('selector').wysiwyg('setContent',newContent);

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

I would like to overwrite the original 'setContent' with my own code, just for this one element.

Thanks


回答1:


(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);

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.



来源:https://stackoverflow.com/questions/2644638/overriding-jquery-plugin-methods-as-default-and-for-single-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!