HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
/*第一组*/
.btn button{}
.btn button.active{ background: #f00; }
.obox li{ display: none; }
.obox li.active{ display: block; }
/*继承出来的*/
.btns button{}
.btns button.active{ background: #f00; }
.oboxs li{ display: none; }
.oboxs li.active{ display: block; }
</style>
</head>
<body>
<!-- 第一组 -->
<div class="btn">
<button type="button" class="active">按钮1</button>
<button type="button">按钮2</button>
<button type="button">按钮2</button>
</div>
<div class="obox">
<ul>
<li class="active">内容1</li>
<li>内容2</li>
<li>内容3</li>
</ul>
</div>
<!-- 继承出来的 -->
<div class="btns">
<button type="button" class="active">按钮1</button>
<button type="button">按钮2</button>
<button type="button">按钮2</button>
</div>
<div class="oboxs">
<ul>
<li class="active">内容1</li>
<li>内容2</li>
<li>内容3</li>
</ul>
</div>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="tab.js"></script><!-- 外部引入插件 -->
<script type="text/javascript" src="limitTab.js"></script><!-- 再继承一次tab的方法 -->
<script type="text/javascript">
$(function(){
new Tab(".btn",".obox");
new LimitTab(".btns",".oboxs");
})
</script>
</body>
</html>
自己写的tab.js(prototype继承相关)
function Tab(btnName,boxName) {
this.btnName = btnName;
this.boxName = boxName;
// console.log(this)
this.myMethods();
}
Tab.prototype.myMethods = function(){
var _this = this;
$(this.btnName+" button").each(function(index){
$(this).click(function(){
$(this).addClass("active").siblings().removeClass("active");
$(_this.boxName+" li").eq(index).addClass("active").siblings().removeClass("active");
})
})
}
limitTab.js(call()方法相关)
// console.log(Tab.prototype)
function LimitTab(btnName,boxName) {
Tab.call(this,btnName,boxName);//继承原来tab插件的属性和方法,call
}
for (var i = 0; i < Tab.prototype.length; i++) {
LimitTab.prototype[i]=Tab.prototype[i];
}