我將如何調用方法showAlert()我試圖存儲一個引用插件與此和$(this)內的自變量,但似乎沒有工作。我在button click事件中調用showAlert。範圍問題裏面的jQuery插件
(function($){
//Add your default settings values here
var settings = {
//Replace with your own settings
testValue:"FOO"
}
var selectedArr=[];
var self=this;
var methods = {
init : function(options){
return this.each(function(){
//if options exists, let's merge them with our default settings
if(options){
$.extend(settings,options);
}
var $this=$(this);
var button = $this.find('.schedule .time a');
button.click(function(evt){
evt.preventDefault();
selectedArr.push($(this).addClass('selected'));
$(this).parent().find('input').attr('checked','checked');
self.showAlert();
})
});
}
}
showAlert: function(){
alert("Please select an alternitive");
}
//Setting namespace. Under no circumstance should a single plugin ever claim more than one namespace in the jQuery.fn object.
//See http://docs.jquery.com/Plugins/Authoring for an explaination of the method used here
$.fn.chooseAppointment = 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.chooseAppointment');
}
};
})(jQuery);
好吧,但我怎麼會從按鈕事件 – Chapsterj
叫它啊有你不需要使用它。 – Chapsterj
爲什麼你需要在它之前使用var?如果我像在我的例子中那樣忽略var,這意味着什麼。那麼在沒有var的情況下,它的範圍是什麼 – Chapsterj