我想创建我的第一个jQuery插件,并且我正在创建this.each is not a function
错误,它在bindUi
方法中引用此行return this.each(function() {
。我认为this
指的是“插件被调用的jQuery对象”,在我的情况下,它将是我在使用$('#location').locationSearch()
创建插件时使用的#location
节点,对不对?页面加载时出现此错误。jQuery插件'this.each不是函数'
这里是我的插件代码:
(function($) {
var methods = {
init : function(options) {
return this.each(function() {
var settings = {
//none yet
};
if (options) {
$.extend(settings, options);
}
methods.log('init');
//attach events
methods.bindUi();
});
},
destroy : function() {
return this.each(function(){ });
},
bindUi : function() {
return this.each(function() {
methods.log('bindUi');
this.click(function() {
methods.log('clicked');
});
});
},
log : function(text) {
if (window.console && window.console.log) {
window.console.log(text);
};
}
} // end methods
jQuery.fn.locationSearch = 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 on jQuery.tooltip');
}
};
})(jQuery);
我也是,如果我的方法,每个人都需要,如果仅init和destroy方法,用它来使用return this.each(function() { ..
还是有点糊涂?
我用'methods.bindUi.apply(this);'替换'methods.bindUi();'时仍然出现同样的错误。 – imns
@bababa:请参阅我的更新。 –
修复它,现在一切正常完美......我正在写一些代码。谢谢! – imns