使這個代碼有效的正確方法是什麼?jquery中未定義變量的屬性
$(function(){
var base = {
$wrapper: $('.homepage_main'),
$main: base.$wrapper.find('#primary').find('.home_slides').closest('[data-id="Colleague"]'),
$panel: base.$wrapper.find('#sidebar').find('.home_slides').closest('[data-id="Colleague"]'),
Template: {
$img: function() { return $('img'); }
},
Modal: {
$modalInterrupt: $('#searching_interruption'),
Suggestion: {
$self: $('#suggested_colleague'),
$loading: base.Modal.Suggestion.$self.find('.gif_loading'),
$paging: base.Modal.Suggestion.$self.find('.pagination'),
$itemContainer: base.Modal.Suggestion.$self.find('.request_items'),
$itemClone: base.Modal.Suggestion.$itemContainer.find('[data-id="Clonable"]').clone().removeAttr('data-id').removeClass('hide'),
$lblCount: base.Modal.Suggestion.$self.find('[data-id="SuggestCount"]'),
$listItems: function() {
return base.Modal.Suggestion.$itemContainer.find('.coll_panel_content:not([data-id="Clonable"])');
}
}
}
};
});
我得到Uncaught TypeError: Cannot read property '$wrapper' of undefined
當你看谷歌瀏覽器的控制檯。
我試着拔出$wrapper
,現在我得到新的錯誤:
Uncaught TypeError: Cannot read property 'Modal' of undefined
我做了這個方法,因爲它更易於管理。例如,如果我在我的html頁面上更改了一些類名或id,我只需要在我的jquery代碼中修改1個特定變量,一切都會再次正常。如果你知道更好的方法,請分享給我。
因爲你無法訪問對象,也不要在其創建其屬性。嘗試另一種方法,如關閉,構造函數或其他。 – Joseph 2013-04-04 06:16:40
所有對'base.XXX'的引用都失敗了,因爲'base'變量還沒有被賦值。 – Barmar 2013-04-04 06:17:24