es6类方法面对对象编程–文本自定义选项卡案例
效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200818165226333.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pva2VyTHpnNQ==,size_16,color_FFFFFF,t_70#pic_center)
HTML
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>面对对象编程-选项栏案例</title>
</head>
<body>
<div class="box" id="box">
<nav class="firstnav">
<ul>
<li class="active"><span class="spanText">选项1</span><span class="close">x</span></li>
<li><span class="spanText">选项2</span><span class="close">x</span></li>
</ul>
<div class="add">
<span>+</span>
</div>
</nav>
<div class="second" id="second">
<section class="section">内容1</section>
<section>内容2</section>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
CSS
代码
*,
ul,
li {
margin: 0;
padding: 0;
}
.box {
width: 800px;
height: 300px;
border: 1px solid red;
margin: 50px auto;
}
.firstnav {
width: 100%;
height: 40px;
border-bottom: 1px solid #ddd;
display: flex;
position: relative;
}
ul {
height: 100%;
width: 800px;
font-size: 0;
}
ul li {
list-style: none;
display: inline-block;
border-right: 1px solid #ddd;
height: 40px;
width: 90px;
text-align: center;
position: relative;
line-height: 38px;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
ul li input {
font-size: 16px;
padding: 8px 4px;
width: 65px;
border-radius: 5px;
}
ul li span {
font-size: 16px;
}
li .close {
width: 12px;
height: 12px;
position: absolute;
overflow: hidden;
right: 0px;
font-size: 12px;
color: #fff;
border: 1px solid #333;
background-color: #333;
border-radius: 0px 0px 0 13px;
line-height: 9px;
text-align: right;
}
.active {
background-color: #ddd;
}
.add {
font-weight: 300;
font-size: 20px;
height: 27px;
width: 27px;
border: 1px solid #ddd;
text-align: center;
position: absolute;
right: 0;
margin: 5px;
}
.add span {
text-align: center;
}
.second {
width: 100%;
height: 258px;
}
section {
width: calc(100% - 40px);
height: calc(100% - 40px);
display: none;
overflow: hidden;
white-space: pre-wrap
}
.section {
padding: 20px;
display: block;
}
section textarea {
width: 100%;
height: 100%;
text-indent: 20px;
font-size: 16px;
resize: none;
}
javascript
代码
let that
class Tab {
constructor(id) {
this.add = document.querySelector('.add')
this.ul = document.querySelector('.firstnav ul')
this.second = document.querySelector('#second')
this.add.onclick = this.addTab
that = this
this.init()
}
init() {
this.lis = document.querySelectorAll('.firstnav li')
this.sections = document.querySelectorAll('#second section')
this.span = document.querySelectorAll('li .close')
this.span = document.querySelectorAll('li .close')
this.spanText = document.querySelectorAll('.spanText')
for (let j = 0; j < this.lis.length; j++) {
this.lis[j].index = j
this.lis[j].onclick = this.toggleTab
this.span[j].onclick = this.deleteTab
this.spanText[j].ondblclick = this.editTabTitle
this.sections[j].ondblclick = this.editTabContent
}
}
addEvent() {
for (let i = 0; i < that.lis.length; i++) {
that.lis[i].className = ''
that.sections[i].className = ''
}
}
toggleTab() {
that.addEvent()
this.className = 'active'
that.sections[this.index].className = 'section'
}
addTab() {
if (that.span.length >= 8) return alert('已经最胖了,不要再加了')
let random = Math.random()
that.addEvent()
let li = '<li class="active"><span class="spanText">新选项</span><span class="close">x</span></li>'
let section = '<section class="section">内容' + random + '</section>'
if (that.lis.length !== 0) {
that.lis[that.lis.length - 1].insertAdjacentHTML('afterend', li)
that.sections[that.sections.length - 1].insertAdjacentHTML('afterend', section)
} else {
that.ul.innerHTML = li
that.second.innerHTML = section
}
that.init()
}
deleteTab(e) {
e.stopPropagation()
let ins = this.parentNode.index
this.parentNode.remove()
that.sections[ins].remove()
that.init()
this.active = document.querySelector('.active')
if (this.active) return
if (ins === 0) {
that.lis[0].className = 'active'
that.sections[0].className = 'section'
}
ins--
that.lis[ins] && that.lis[ins].click()
}
editTabTitle(e) {
e.stopPropagation()
this.innerHTML = '<input type="text" value="' + this.innerText + '">'
this.input = document.querySelector('input')
this.input.select()
this.input.onkeydown = function (e) {
if (e && e.keyCode === 13) {
this.onblur = '';
that.outBlurInput.call(this)
}
}
this.input.onblur = that.outBlurInput
console.dir(this);
}
editTabContent(e) {
e.stopPropagation()
this.innerHTML = '<textarea>' + this.innerText + '</textarea>'
this.textarea = document.querySelector('textarea')
this.textarea = document.querySelector('textarea')
this.textarea.select()
this.textarea.onkeydown = function (e) {
if (e && e.keyCode === 13) {
this.onblur = '';
that.outBlurText.call(this)
}
}
this.textarea.onblur = that.outBlurText
}
outBlurInput() {
if (this.value.length > 5) {
this.parentNode.innerHTML = this.value.substring(0, 5)
alert('文字长度不能大于5')
} else if (this.value.length <= 0) {
this.parentNode.innerHTML = '请填写'
alert('内容不能为空')
}
console.log(this.parentNode);
this.parentNode.innerHTML = this.value
}
outBlurText() {
this.parentNode.innerHTML = this.value
}
}
let table = new Tab()
作者:jokerLzg5
本文禁止转载