我有以下jquery插件代码,我在其上工作,但它给了我Uncaught TypeError: Cannot read property 'retValue' of undefined
。以下是我的代码在jQuery插件中获取未定义错误的属性'retValue'
(function ($) {
var Plugin = {
init: function (options, elem) {
var self = this;
self.elem = elem;
self.$elem = $(elem);
this.search = (typeof options === 'string') ? options:options.retValue;
this.options = $.extend({}, $.fn.plugin.options, options);
}
};
$.fn.plugin = function (options) {
return this.each(function() {
var plugin = Object.create(Plugin);
plugin.init(options, this);
});
};
$.fn.plugin.options = {
retValue: 'value',
};
})(jQuery);
$('#selector').plugin();
这里我没有在边界()中传递参数。所以我想让我的插件获得默认的retValue
,但它给了我上面的错误。我该如何解决这个问题?
UPDATE
我想在3名以下的方法来使用这个插件。
$('#selector').plugin(); // does not work
$('#selector').plugin('string'); // works
$('#selector').plugin({key1: 'value1', key2: 'value2'}); // works
我该如何做第一个工作?
任何人谁可以帮我? – 2619