我爱编程

JQ选项卡的一个封装

2018-03-25  本文已影响42人  加冰宝贝

script部分

function Tab(option){

        this.root=$(option.root);

        this.tabTag=this.root.find('#tabon li');

        this.hidden=this.root.find('.hidden');

        this.init();   

}

Tab.prototype={//prototype对象是实现面向对象的重要机制

         init:function(){

                    var that=this;

                            this.tabTag.each(function(i){         //each()方法为每个匹配元素规定要运行的函数

                                            $(this).click(function(){

                                                        that.tabTag.removeClass("cur");

                                                          $(this).addClass('cur');

                                                           that.hidden.eq(i).show().siblings().hide();

                                              }) 

                               })         

           }  

}

$(function(){

new Tab({'root':$('.tab')});

//root选择器选取文档的根元素,new会创建pre的对象,没有new的话就没有任何可返回的值或对象,所以是undefined。

})

上一篇 下一篇

猜你喜欢

热点阅读