我有以下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