我正在努力学习更好地构建我的代码,并在学习了一些教程后开始使用下面的模式。
我以这种方式构建了许多常见的 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();
});