我試圖學習更好地構建我的代碼,並且在遵循一些教程之後開始使用下面的模式。
我已經用這種方式構建了許多常見UI小部件,但我已經擊中了我的第一面牆,下面的.each()循環似乎實際上並不是循環遍歷每個項目正在應用所需的操作,就好像它在一次迭代中對所有項目執行操作一樣。
我讀了一些關於$ .each和objects的內容,但我不確定$ .each,甚至是否是我應該去的方向。
jQuery(document).ready(function($) {
var tabs = {
trig : $('nav.tab-triggers'),
content : $('section.tabs-content'),
init : function() {
tabs.address([tabs.trig]);
tabs.address([tabs.content]);
},
address : function(item) {
//for every instance of the item
$(item).each(function() {
var i = 1;
$(this).addClass('tabs-' + i);
// for ever child element in this instance of the item
$(this).children().each(function() {
var count = 1;
if ($(this).parent().is('nav')) {
$(this).attr('href', '#tab-' + count);
} else {
$(this).attr('id', 'tab-' + count);
}
count++;
});
i++;
});
},
display : function() {
//show hide stuff here.
}
}
tabs.init();
});
我注意到你用你自己的櫃檯,就可以使用 $(項目)。每個(函數(指數){ }); 不理解你的問題的其餘部分 –
嘿感謝您的輸入,如果我可以進一步解釋,基本上所有的孩子獲得相同的href(在導航的情況下)或身份證,而不是遞增的 - 好像它是處理所有與每個元素相對的匹配元素,因爲它應該循環。 – pushplaybang
所以你得到所有元素相同的ID或HREF? –