面向对象tab栏,完成增删改查
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h4>js面向对象 动态添加标签页</h4>
<div class="tabsbox" id="tab">
<!--tab标签-->
<nav class="firstnav">
<ul>
<li class="liactive"><span>测试1</span><span class="iconfont icon-gunbi"></span></li>
<li><span>测试2</span><span class="iconfont icon-gunbi"></span></li>
<li><span>测试3</span><span class="iconfont icon-gunbi"></span></li>
</ul>
<div class="tabadd">
<span>+</span>
</div>
</nav>
<!--tab内容-->
<div class="tabscon">
<section class="conactive">测试1</section>
<section>测试2</section>
<section>测试3</section>
</div>
</div>
<script src="tab.js"></script>
</body>
</html>
JS代码:
var that;
class Tab{
constructor(id){
//获取元素
that=this;
this.main=document.querySelector(id);
this.add=this.main.querySelector('.tabadd');
//li的父元素
this.ul=this.main.querySelector('.firstnav ul:first-child');
//section父元素
this.fsection=this.main.querySelector('.tabscon');
this.init();
}
init(){
this.updataNode();
//init 初始化操作让相关的元素绑定事件
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.span[i].ondbclick=this.editTab;
this.sections[i].ondbclick=this.editTab;
}
}
//获取所有的li和section
updataNode(){
this.lis=this.main.querySelector('li');
this.sections=this.main.querySelector('section');
this.remove=this.main.querySelectorAll('.icon-guanbi');
this.spans=this.main.querySelectorAll('.firstnav li span:first-child');
}
//1.切换功能
toggleTab(){
that.clearClass();
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();
//(1)创建li元素和section元素
var li='<li class='liactive'><span>新选项卡</span><span class='iconfont icon-guanbi'></span></li>'
var section='<section class='conactive'>测试1</section>';
//(2)把这两个元素追加到对应的父元素里面
that.ul.insertAdjacentHTML('befo reend',li);
that.fsection.insertAdjacentHTML('beforeend',section);
that.init();
}
//3.删除功能
removeTab(){
e.stopPropagation();//阻止冒泡,防止触发li的切换点击事件
var index=this.parentNode.index;
//根据索引号删除对应的li和section
that.lis[index].remove();
that.section[index].remove();
that.init();
//当我们删除的不是选定状态的li的时候,原来的转中状态li保持不变
if(document.querySelector('.liactive')) return;
//当我们删除了选中状态的这个li的时候,让他的其哪一个li处于选定状态
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'/>';
var input=this.children[0];
input.value=str;
input.select();//文本框里面的文字处于选定状态
//当我们离开文本框的时候把文本框里面的内容给span
input.onblur=function(){
this.parentNode,innerHTML=this.value;
};
//按下回车也可以把文本框里面的值给span
input.onkeyup=function(e){
if(e.onkeyup==13){
//手动调用表单失去焦点事件 不需要鼠标离开操作
this.blur();
}
}
}
}
new Tab('#tab');
网友评论