1
我想在jQuery中構建一個小部件來處理內容加載到頁面上的某些div。我這樣做的原因是保持我的代碼乾爽。我應該提到我使用HeadJS進行JavaScript加載。無法讀取未定義的屬性'url'。 jQuery加載小部件失敗
這是我的小部件代碼:
(function ($, window, document, undefined) {
$.widget ("my.contentloader", {
options: {
loadingMessage: true,
errorDiv: '#error',
contentDiv: '#content'
},
_create: function() {
$.ajax ({
type: "POST",
url: self.options.url,
data: {limit: self.options.params.limit, offset: self.options.params.offset},
beforeSend: function (html) {
// Check if loading message should be displayed
if (self.options.loadingMessage) {
$(self.options.contentDiv).html ("<div id='loading'>Loading</div>");
}
},
success: function (html) {
if (self.options.loadingMessage) {
$('#loading').remove();
}
$(self.options.contentDiv).html (html);
},
error: function (html) {
$(self.options.errorDiv).html (html);
}
});
},
_setOption: function (key, value) {
this.options[key] = value;
$.Widget.prototype._setOption.apply (this, arguments);
}
});
})(jQuery, window, document);
包括我這個小部件文件和jQuery調用頁面上。然後,我使用的插件是這樣的:
$(window).contentloader ({
url: 'loading/content/url'
});
的問題是,我得到這個錯誤:
Uncaught TypeError: Cannot read property 'url' of undefined
完美地工作。謝謝! –