效果图
具有增加删除效果
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/index.css">
<link rel="stylesheet" href="./fonts/iconfont.css">
</head>
<body>
<div class="tab-box" id="tab">
<!-- tab部分 -->
<nav>
<ul>
<li class="li-active">
<span>标签1</span>
<span class="iconfont icon-guanbi"></span>
</li>
<li>
<span>标签2</span>
<span class="iconfont icon-guanbi"></span>
</li>
<li>
<span>标签3</span>
<span class="iconfont icon-guanbi"></span>
</li>
</ul>
<!-- 添加按钮 -->
<div class="tab-add">
<span>+</span>
</div>
</nav>
<!-- 内容区域 -->
<div class="tab-con">
<section class="con-active">标签1内容部分</section>
<section>标签2内容部分</section>
<section>标签3内容部分</section>
</div>
</div>
<script src="./js/index.js"></script>
</body>
</html>
css代码
* {
margin: 0px;
padding: 0px;
}
ul {
list-style: none;
}
.tab-box {
width: 800px;
height: 400px;
border: 1px solid skyblue;
margin: 0 auto;
}
.tab-box nav {
width: 100%;
height: 60px;
border-bottom: 1px solid #ccc;
display: flex;
justify-content: space-between;
align-items: center;
}
.tab-box nav ul {
width: 90%;
height: 100%;
display: flex;
}
.tab-box nav ul .li-active {
border-bottom: 2px solid white;
}
.tab-box nav ul li {
width: 100px;
height: 60px;
text-align: center;
line-height: 60px;
border-right: 1px solid #ccc;
position: relative;
}
.tab-box nav ul li input {
width: 80px;
height: 30px;
}
.tab-box nav ul li .iconfont {
position: absolute;
top: -22px;
right: 0px;
}
.tab-box nav .tab-add {
width: 15px;
height: 15px;
border-radius: 3px;
border: 1px solid #ccc;
text-align: center;
line-height: 15px;
margin-right: 10px;
}
.tab-box nav .tab-add:hover {
cursor: pointer;
}
.tab-box .tab-con {
width: 100%;
height: 339px;
/* background-color: pink; */
}
.tab-box .tab-con section {
width: 100%;
height: 100%;
display: none;
padding: 20px;
box-sizing: border-box;
}
.tab-box .tab-con section textarea {
width: 100%;
height: 100%;
font-size:40px;
letter-spacing: 1em;
font-family: "华文行楷";
resize: none;
padding: 4px;
box-sizing: border-box;
}
.tab-box .tab-con .con-active {
display: block;
}
js代码
// 面向对象的变成思想
// 把事务拆分成一个个对象 然后分工合作
var that;
class Tab {
constructor(id) {
that = this;
this.tabContent = document.querySelector(id);
this.tab_con = document.querySelector('#tab .tab-con');
// 获取元素
this.ul = document.querySelector('#tab ul');
this.tab_add = document.querySelector('#tab .tab-add');
this.icon = document.querySelectorAll('#tab ul li .iconfont');
this.init();
}
//初始化事件
init() {
this.update();
console.log(1)
for (var i = 0; i < this.lis.length; i++) {
console.log(this.tab);
this.lis[i].index = i;
this.lis[i].onclick = this.tab;
this.lis[i].ondblclick = this.liEdit;
this.section[i].ondblclick = this.secEdid;
this.icon[i].onclick = this.spanMove;
}
this.tab_add.onclick = this.add;
}
// 更新
update() {
console.log(2)
this.lis = document.querySelectorAll('#tab ul li');
this.section = document.querySelectorAll('#tab .tab-con section');
this.icon = document.querySelectorAll('#tab ul li .iconfont');
}
// 切换事件
tab() {
that.siblings();
this.className = "li-active";
console.log(this.index)
that.section[this.index].className = "con-active";
}
// 排他
siblings() {
for (var i = 0; i < this.lis.length; i++) {
this.lis[i].className = "";
this.section[i].className = "";
}
}
// 添加
add() {
that.siblings();
// console.log(1)
var num = that.lis.length;
num++
that.ul.innerHTML += `
<li class='li-active'>
<span>标签${num}</span>
<span class="iconfont icon-guanbi"></span>
</li >
`
that.tab_con.innerHTML += `
<section class='con-active'>标签${num}内容部分</section>
`
that.update();
that.init();
}
liEdit() {
this.innerHTML = `<input type='text' value=${this.innerText} class='txt'>`
var txt = document.querySelector(".txt");
txt.onblur = function () {
// var text = txt.value;
this.parentNode.innerHTML = `
<li>
<span>${this.value}</span>
<span class="iconfont icon-guanbi"></span>
</li>
`
}
}
secEdid() {
this.innerHTML = `
<textarea class='www'>${this.innerText}</textarea>
`
var txt = document.querySelector(".www");
txt.onblur = function () {
this.parentNode.innerHTML = `
<section class='con-active'>${this.value}</section>
`
}
}
// 删除
spanMove(){
this.parentNode.parentNode.removeChild(this.parentNode);
that.index=this.parentNode.index;
that.sectionMove()
}
sectionMove(){
this.section[that.index].remove();
}
}
var p1 = new Tab('#tab');
console.log(p1)