How to extend jQuery plugin's methods ?

In the following code i tried to extend my test plugin. I want to add a new method definition to existing plugin. (function($) { var settings = { bg: 'white' }; var methods = { init: function(options) { options = $.extend({}, options, settings); return this.each(function () { $(this).data("test", options); }); }, whiten: function() { var options = this.data("test"); this.css('background-color', options.bg); } }; $.fn.test = function(method) { if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { return methods.init.apply(this, arguments); } else { $.error('Method ' + method + ' does not exist'); } } })(jQuery); // New Method Definition var methods = { newMethod: function(){ alert('umut'); } }; // Extending test plugin with newMethod $.fn.extend(true,$.fn.test, methods ); $(document).ready(function() { $('ul').test().css('background-color', 'wheat'); $('#go').click(function() { $('ul').test("whiten"); // Calling newMethod $('ul').test("newMethod"); }); }); But i got following error at firebug console: > uncaught exception: Method newMethod does not exist How can i extend var methods = { } block inside my test plugin? Here is the jsfiddle [link](http://jsfiddle.net/gurkavcu/NSVVv/1/).

以上就是How to extend jQuery plugin's methods ?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏