美文网首页我爱编程
JQ选项卡的一个封装

JQ选项卡的一个封装

作者: 加冰宝贝 | 来源:发表于2018-03-25 19:37 被阅读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。

})

相关文章

网友评论

    本文标题:JQ选项卡的一个封装

    本文链接:https://www.haomeiwen.com/subject/jgrkcftx.html