<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>选项卡插件的写法</title>
<style>
#tab div{ width:200px; height:200px; border:1px #000 solid; display:none;}
#tab .active{ background:red;}
#tab2 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
#tab2 .active{ background:red;}
</style>
<script src="jquery-1.11.1.js"></script>
<script>
//自定义事件:
//beforeChange
//afterChange
//配置信息
$(function(){
$('#tab').tabs();
$('#tab2').tabs({
heads : ['体育','娱乐','新闻','视频'],
bodys : ['tab1体育','tab1娱乐','tab1新闻f','tab1视频'],
events : 'mouseover'
});
$('#tab2').on('beforeChange',function(){
alert( $('#tab2').find('div:visible').html() );
});
$('#tab2').on('afterChange',function(){
alert( $('#tab2').find('div:visible').html() );
});
});
//闭包写法 (注意前后加括号)
(function($){
function fnTab(ele, options){
this.$element = ele;
this.defaults = {
heads : ['1','2','3'],
bodys : ['1111111','2222222','3333333'],
events : 'click'
};
this.settings = $.extend({} , this.defaults , options);
}
fnTab.prototype.init = function(){
this.create();
this.bind();
}
fnTab.prototype.create = function(){
for(var i=0;i<this.settings.heads.length;i++){
var $input = $('<input type="button" value="'+ this.settings.heads[i] +'">');
if(i==0){
$input.attr('class','active');
}
this.$element.append( $input );
}
for(var i=0;i<this.settings.bodys.length;i++){
var $div = $('<div>'+ this.settings.bodys[i] +'</div>');
if(i==0){
$div.css('display','block');
}
this.$element.append( $div );
}
}
fnTab.prototype.bind = function (){
var that = this;
that.$element.find('input').on(this.settings.events,function(){
that.$element.trigger('beforeChange');
that.$element.find('input').attr('class','');
$(this).attr('class','active');
that.$element.find('div').css('display','none');
that.$element.find('div').eq( $(this).index() ).css('display','block');
that.$element.trigger('afterChange');
});
}
//在插件中使用Tab对象
$.fn.tabs = function(options) {
//创建Tab的实体
var Tab = new fnTab(this, options);
//调用其方法
return Tab.init();
}
})(jQuery);
</script>
</head>
<body>
<div id="tab">
<!--<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display:block">1111111</div>
<div>2222222</div>
<div>3333333</div>-->
</div>
<div id="tab2"></div>
</body>
</html>