#控制多個選項卡自動播放
```
<style>
#div1 div, #div2 div {
width: 200px;
height: 200px;
border: 1px #000 solid;
display: none;
}
.active {
background: red;
}
</style>
<script>
window.onload = function () {
var t1 = new Tab('div1');
t1.init();
var t2 = new Tab('div2');
t2.init();
t2.autoPlay();
};
function Tab(id) {
this.oParent = document.getElementById(id);
this.aInput = this.oParent.getElementsByTagName('input');
this.aDiv = this.oParent.getElementsByTagName('div');
this.iNow = 0;
}
Tab.prototype.init = function () {
var _this = this;
for (var i = 0; i < this.aInput.length; i++) {
this.aInput[i].index = i;
this.aInput[i].onclick = function () {
_this.change(this);
};
}
};
Tab.prototype.change = function (obj) {
for (var j = 0; j < this.aInput.length; j++) {
this.aInput[j].className = '';
this.aDiv[j].style.display = 'none';
}
obj.className = 'active';
this.aDiv[obj.index].style.display = 'block';
};
Tab.prototype.autoPlay = function(){
var _this = this;
setInterval(function () {
if(_this.iNow == _this.aInput.length - 1){
_this.iNow = 0;
}else{
_this.iNow++;
}
for (var j = 0; j < _this.aInput.length; j++) {
_this.aInput[j].className = '';
_this.aDiv[j].style.display = 'none';
}
_this.aInput[_this.iNow].className = 'active';
_this.aDiv[_this.iNow].style.display = 'block';
}, 2000);
};
</script>
<div id="div1">
<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">
<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>
```
- 01 JS面向對象及組件開發
- 02 傳統的過程式編寫選項卡
- 03 用面向對象封裝通用選項卡
- 04 控制多個選項卡自動播放
- 05 用面向對象編寫拖拽
- 06 JS面向對象及組件開發
- 07 hasOwnProperty和constructor的使用
- 08 instanceof運算符的使用
- 09 利用toString做類型判斷
- 10 什么是面向對象的繼承
- 11 面向對象之拷貝繼承
- 12 編寫繼承的拖拽
- 13 繼承的其他形式之類式繼承
- 14 繼承的其他形式之原型繼承
- 15 組件開發是什么
- 16 給拖拽組件配置不同參數
- 17 封裝彈框組件
- 18 使用對象標記已彈出彈框
- 19 復雜組件開發之自定義事件
- 20 原生JS實現自定義事件
- 21 自定義事件實例
- 22 基于JQ的選項卡組件開發