[JS]基于JQ库的组件开发和插件扩展

写了一个小的demo,底层库用的是JQ

把配置,方法,事件很好的进行了区分

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.tabs input.active{ background:red; }
.tabs div{ width:200px; height:200px; border:1px #000 solid; display:none;}
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script>

$(function(){
    var t1 = new CreateTab();
    t1.init({
        id : 'div1'
    });
    var t2 = new CreateTab();
    t2.init({
        id : 'div2',
        events : 'mouseover'
    });
    var t3 = new CreateTab();
    t3.init({
        id : 'div3',
        events : 'mouseover',
        delay : 300
    });
    
    $('#input1').click(function(){
        t3.select(2);
        alert( t3.currentContent() );
    });
    
    var t4 = new CreateTab();
    t4.init({
        id : 'div4',
        events : 'click'
    });
    $(t4).on('toBeforeChange',function(){
        //alert( t4.currentContent() );
    });
    $(t4).on('toChange',function(){
        //alert( t4.currentContent() );
        alert( t4.currentTitle() );
    });
    
    $.extend(CreateTab.prototype,{   //插件扩展
        currentTitle : function(){
            return $(this.id).find('input').eq( this.iNow ).val();
        }
    });
    
    
    
});

function CreateTab(){  //组件开发
    this.id = null;
    this.aInput = null;
    this.aDiv = null;
    this.iNow = 0;
    this.defaults = {
        events : 'click',
        delay : 0
    };
}
CreateTab.prototype = {
    init : function(options){
        var This = this;
        options = $.extend(this.defaults,options);
        this.id = '#' + options.id;
        $(this.id).find('input').on(options.events,function(){
            var obj = this;
            if(options.events=='mouseover' && options.delay){
                obj.timer = setTimeout(function(){
                    This.change(obj);
                },options.delay);
                
                $(obj).mouseout(function(){
                    clearTimeout(obj.timer);
                });
            }
            else{
                This.change(obj);
            }
            
        });
    },
    change : function(obj){
        
        $(this).trigger('toBeforeChange');
        
        $(this.id).find('input').attr('class','');
        $(this.id).find('div').css('display','none');
        
        $(obj).attr('class','active');
        $(this.id).find('div').eq( $(obj).index() ).css('display','block');    
        
        this.iNow = $(obj).index();
        
        $(this).trigger('toChange');
        
    },
    select : function(index){
        $(this.id).find('input').attr('class','');
        $(this.id).find('div').css('display','none');
        
        $(this.id).find('input').eq(index).attr('class','active');
        $(this.id).find('div').eq(index).css('display','block');    
        
        this.iNow = index;
        
    },
    currentContent : function(){
        return $(this.id).find('div').eq( this.iNow ).html();
    }
};
</script>
</head>

<body>
<div id="div1" class="tabs">
    <input class="active" type="button" value="1" />
    <input type="button" value="2" />
    <input type="button" value="3" />
    <div style="display:block">111</div>
    <div>222</div>
    <div>333</div>
</div>
<div id="div2" class="tabs">
    <input class="active" type="button" value="1" />
    <input type="button" value="2" />
    <input type="button" value="3" />
    <div style="display:block">111</div>
    <div>222</div>
    <div>333</div>
</div>
<div id="div3" class="tabs">
    <input class="active" type="button" value="1" />
    <input type="button" value="2" />
    <input type="button" value="3" />
    <div style="display:block">111</div>
    <div>222</div>
    <div>333</div>
</div>

<input type="button" id="input1" value="方法测试" />

<div id="div4" class="tabs">
    <input class="active" type="button" value="1" />
    <input type="button" value="2" />
    <input type="button" value="3" />
    <div style="display:block">111</div>
    <div>222</div>
    <div>333</div>
</div>

</body>
</html>

 

转载于:https://www.cnblogs.com/uedqd/archive/2013/02/23/2923099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值