html:
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
main {
width: 700px;
margin: 0 auto;
}
li {
list-style: none;
}
.tabsbox {
width: 700px;
height: 700px;
border: 1px solid #ccc;
}
.firstnav {
width: 700px;
height: 50px;
line-height: 50px;
}
.firstnav ul {
text-align: center;
}
.firstnav ul li {
position: relative;
float: left;
width: 50px;
height: 50px;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
list-style: none;
}
.tabadd {
float: right;
width: 30px;
height: 30px;
margin: 10px;
text-align: center;
line-height: 30px;
border: 1px solid #ccc;
}
.tabscon section {
margin-top: 100px;
height: 100px;
display: none;
}
.tabscon .conactive {
display: block;
}
.icon-guanbi {
position: absolute;
top: -20px;
right: 0;
height: 10px;
width: 10px;
font-size: 5px;
color: red;
border-radius: 0 0 0 10px;
}
.firstnav .liactive {
border-bottom: none;
}
</style>
</head>
<body>
<main>
<h4>
Js面向对象 动态添加标签页
</h4>
<div class="tabsbox" id="tab">
<!-- tab标签 -->
<nav class="firstnav">
<ul>
<li><span>测试1</span><span class="iconfont icon-guanbi">×</span></li>
</ul>
<div class="tabadd">
<span>+</span>
</div>
</nav>
<!-- tab内容 -->
<div class="tabscon">
<section>测试仪</section>
<section>测试2</section>
<section>测试3</section>
</div>
</div>
</main>
</body>
<script src="js/tab.js"></script>
</html>`
js:
var that;
class Tab {
constructor(id) {
that = this;
this.main = document.querySelector(id);
this.add = this.main.querySelector('.tabadd');
this.ul = this.main.querySelector('.firstnav ul');
this.tabscon = this.main.querySelector('.tabscon')
this.init();
}
init() {
this.updateNode();
this.add.onclick = this.addTab;
// 初始化操作让相关的元素绑定事件
for (var i = 0; i < this.lis.length; i++) {
this.lis[i].index = i;
this.lis[i].onclick = this.toggleTab;
this.remove[i].onclick = this.removeTab;
this.spans[i].ondblclick = this.editTab;
this.sections[i].ondblclick = this.editTab;
}
}
updateNode() {
this.lis = this.main.querySelectorAll('li');
this.sections = this.main.querySelectorAll('section');
this.remove = this.main.querySelectorAll('.iconfont');
this.spans = this.main.querySelectorAll('.firstnav li span:first-child')
}
// 1.切换功能
toggleTab() {
that.clearClass();
console.log(this.index);
this.className = 'liactive';
that.sections[this.index].className = 'conactive';
}
clearClass() {
for (var i = 0; i < this.lis.length; i++) {
this.lis[i].className = '';
this.sections[i].className = '';
}
}
// 2.添加功能
addTab() {
that.clearClass();
var random = Math.random();
var section = ' <section class="conactive">测试' + random + '</section>';
var li = ' <li><span>新</span><span class="iconfont icon-guanbi">×</span></li>';
that.ul.insertAdjacentHTML('beforeend', li);
that.tabscon.insertAdjacentHTML('beforeend', section);
that.init();
}
// 3.删除功能
removeTab(e) {
e.stopPropagation();
var index = this.parentNode.index;
that.lis[index].remove();
that.sections[index].remove();
that.init();
console.log(index);
// 当我们删除不是选定状态的li的时候,原先的选定状态不变
if (document.querySelector('.liactive')) return;
index--;
// 手动调用点击事件 不需要鼠标触发
// 前面为真执行后面的调用,为假不执行后面的调用
that.lis[index] && that.lis[index].click();
}
// 4.修改功能
editTab() {
var str = this.innerHTML;
// 双击禁止选中文字
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
this.innerHTML = '<input type="text" style="height:60%;width:60%;"/>';
var input = this.children[0];
input.value = str;
input.select(); //文本框选定状态
// 离开文本框就把文本框的值给文本框
input.onblur = function() {
this.parentNode.innerHTML = this.value;
};
// 按下回车键可以把文本框的值给span
input.onkeyup = function(e) {
// 13是回车键的ASCII的值
if (e.keyCode === 13) {
// 手动调用表单失去焦点事件 不需要鼠标离开操作
this.blur();
}
}
}
}
new Tab('#tab');