最近在重新学习JavaScript,手写了一个tabs标签页。
话不多说,直接开始。
首先,是前端页面。
图1 tabs
先来把tabs分解一下:
图2 tabs分解
首先,一个大的框div,上面红色的框是导航栏nav,导航栏里是一个无序列表ul,里面三个li标签(黄色的框),li标签里两个绿色标签是两个span,一个用来放导航的名字,一个用来放导航关闭的icon,右边是一个button,用来添加新的导航栏及内容;下方是导航栏的内容section。
导航tabs.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> </head> <body> <main> <div class="tabsbox" id="tab"> <!-- tab标签 --> <nav class="firstnav"> <ul> <li class="liactive"><span>测试1</span><span class="icon-guanbi">X</span></li> <li><span>测试2</span><span class="icon-guanbi">X</span></li> <li><span>测试3</span><span class=" icon-guanbi">X</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> </main> <script src="/UploadFiles/2021-04-02/tabs.js">由于给导航栏增删查改的js代码很多,我单独用一个js文件写tabs.js,在tabs.html里引用就行。
实现的导航栏功能有:
1)导航栏的切换功能
2)增加导航栏的功能
3)删除导航栏的功能
tabs.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.updateNode(); this.add.onclick = this.addTab; for(let 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; } } //获取li跟section updateNode(){ this.lis = this.main.querySelectorAll('li'); this.sections = this.main.querySelectorAll('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(let i = 0;i<this.lis.length;i++){ this.lis[i].className = ''; this.sections[i].className = ''; } } //2.添加功能 addTab(){ that.clearClass(); //1)添加li元素和section var random = Math.random() var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi">X</span></li>'; var section = '<section class="conactive">'+random+'</section>'; //2)把两个元素添加到对应的父级元素中 that.ul.insertAdjacentHTML('beforeend',li); that.fsection.insertAdjacentHTML('beforeend',section); that.init(); } //3.删除功能 removeTab(e){ e.stopPropagation(); var index = this.parentNode.index; console.log(index); //删除对应节点 that.lis[index].remove(); that.sections[index].remove(); //删除后及时更新页面中的节点 that.init(); //如果当前页面有选中状态,就不用执行下面的步骤 if(document.querySelector('.liactive')) return; //让index前一个处于选中状态 index--; that.lis[index] && that.lis[index].click(); } //4.修改功能 editTab(){ let str = this.innerHTML; // console.log(str); //禁止双击选中文字 window.getSelection "text">' let input = this.children[0]; input.value = str; input.select(); //input失去焦点后变回span input.onblur = function(){ this.parentNode.innerHTML= this.value; } //按回车也能 input.onkeyup = function(e){ if(e.keyCode == 13){ this.blur();//获得焦点 } } } } new Tab('#tab'); // tab.init();