2
我試圖寫一個基於原型模式的jQuery插件。
我的問題是當我使用$.ajax
函數加載一個xml文件時,我鬆開了this
對象。
例如這項工作:
Plugin.prototype = {
defaults: {
message: 'Hello world!'
},
init: function() {
this.setStyleAndOptions();
return this;
},
setStyleAndOptions: function() {
alert("setStyleAndOptions : ");
}
}
但是,這並不,我得到一個錯誤說 「this.setStyleAndOptions沒有定義」:
Plugin.prototype = {
defaults: {
message: 'Hello world!'
},
init: function() {
$.ajax({
type: "GET",
url: "param.xml",
dataType: "xml",
success: this.initOptions
});
return this;
},
initOptions: function(dataxml) {
// Error occured here, I suppose "this" is not the plugin anymore
this.setStyleAndOptions();
},
setStyleAndOptions: function() {
alert("setStyleAndOptions");
}
}
感謝您的幫助。
謝謝。這是我剛剛宣佈另一個變量的正確方向,因爲在成功函數中,這是函數。但與第一個答案的混合做到了這一點 – user788721