鉴于这种结构:
(function($) {
var defaults = {
dragDelay: 200, // tap/hold this long to activate dragging
feedback: 'glow', // feedback to show drag mode (glow/pulsate)
glowColor: '#fff' // color of the glow if feedback==glow
};
var methods = {
init: function(options) {
return this.each(function() {
options = $.extend({}, defaults, options);
var $this = $(this);
$this.bind('click', methods.doSomething);
});
},
doSomething: function(e) {
//...
}
};
$.fn.myPlug = 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.length > 0 ? arguments : [defaults]);
else
$.error("TypeError: Plugin 'myPlug' has no method '" + method +
"'");
}
})(jQuery);
是否最好使用$(this).data()
来存储数据,或this.someVar = x
?另外,如何让init()方法与其他方法共享'options'?
答案 0 :(得分:0)
如果您正在创建一些可视化小部件,那么您可以创建自定义jQuery UI小部件(它有很好的技术): http://docs.jquery.com/UI_Developer_Guide
但如果没有 - 那么最好使用$(this).data(...)
来存储某些对象的个人数据。
如何获取init()
方法?您也可以使用.data()
方法存储它。