转载地址 https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/0014356468967974219593d94f64d06b370c87fc38eade4000
膜拜的文章
规则:
- 给
$.fn
绑定函数,实现插件的代码逻辑; - 插件函数最后要
return this;
以支持链式调用; - 插件函数要有默认值,绑定在
$.fn.<pluginName>.defaults
上; - 用户在调用时可传入设定值以便覆盖默认值。
- $.fn 函数里面的this 绑定的是 jQ对象, so jQ方法可以直接使用
eg:
构造一个高亮的插件
$.fn.highlight = function (options) { var setting = $.extend({}, $.fn.highlight.defaults, options); this.css("background", setting.background).css("color", setting.color); return this; }; $.fn.highlight.defaults = { background : 'yellow', color : "red" };