面向对象选项卡

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css" media="screen">
.on{background: #f60; color: #fff}
.box div{ 200px;height: 200px;
border:1px solid #000;display: none}
</style>
</head>
<body>
<div id="box" class="box">
<input class="on" type="button" value="aaa">
<input type="button" value="bbb">
<input type="button" value="ccc">
<div style="display: block">aaaa</div>
<div>bbbb</div>
<div>cccc</div>
</div>
<div id="box2" class="box">
<input class="on" type="button" value="aaa">
<input type="button" value="bbb">
<input type="button" value="ccc">
<div style="display: block">aaaa</div>
<div>bbbb</div>
<div>cccc</div>
</div>
<script >
//选项卡
class Tab{
constructor(id){
this.oBox = document.getElementById(id);
this.aBtn = this.oBox.getElementsByTagName('input');
this.aDiv = this.oBox.getElementsByTagName('div');
this.iNow=0;
this.init();
}
init(){
for(let i =0;i<this.aBtn.length;i++){
this.aBtn[i].onclick = function(){
this.iNow=i
; this.hide();
this.show(i);
document.title=this.iNow;
}.bind(this);
}
}
hide(){
for(let i=0;i<this.aBtn.length;i++){
this.aBtn[i].className='';
this.aDiv[i].style.display='none';
}
}
show(index){
this.aBtn[index].className = 'on';
this.aDiv[index].style.display='block';
}
}
//继承
class AutoTab extends Tab{
constructor(id){
super(id);
setInterval(this.next.bind(this),1000)
}
next(){
this.iNow++;
if(this.iNow==this.aBtn.length){this.iNow=0}
this.hide();
this.show(this.iNow);
}
};
window.onload = function(){
new Tab('box');
var at=new AutoTab('box2');
}
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/qdcnbj/p/8073886.html