<div class="container">
<!-- 选项卡结构 -->
<div class="control">
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
</div>
<div class="content">
<div class="box">内容1</div>
<div class="box">内容2</div>
<div class="box">内容3</div>
</div>
</div>
<script>
function Table(btn_selector, content_selector) {
this.btns = document.querySelectorAll(btn_selector);
this.contents = document.querySelectorAll(content_selector);
this.index = 0;
this.bindEvent();
this.changeTable();
}
Table.prototype.bindEvent = function () {
var self = this;
for (var i = 0; i < self.btns.length; i++) {
self.btns[i].index = i;
self.btns[i].onclick = function () {
self.index = this.index;
self.changeTable();
}
}
}
Table.prototype.changeTable = function () {
var self = this;
for (var j = 0; j < self.contents.length; j++) {
self.contents[j].style.display = "none";
}
self.contents[self.index].style.display = "block";
}
var table = new Table(".control button", ".box");
</script>