亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

原生JavaScript寫出Tabs標(biāo)簽頁的實(shí)例代碼

瀏覽:193日期:2023-06-19 09:40:03

最近在重新學(xué)習(xí)JavaScript,手寫了一個(gè)tabs標(biāo)簽頁。

話不多說,直接開始。

首先,是前端頁面。

原生JavaScript寫出Tabs標(biāo)簽頁的實(shí)例代碼

圖1 tabs

先來把tabs分解一下:

原生JavaScript寫出Tabs標(biāo)簽頁的實(shí)例代碼

圖2 tabs分解

首先,一個(gè)大的框div,上面紅色的框是導(dǎo)航欄nav,導(dǎo)航欄里是一個(gè)無序列表ul,里面三個(gè)li標(biāo)簽(黃色的框),li標(biāo)簽里兩個(gè)綠色標(biāo)簽是兩個(gè)span,一個(gè)用來放導(dǎo)航的名字,一個(gè)用來放導(dǎo)航關(guān)閉的icon,右邊是一個(gè)button,用來添加新的導(dǎo)航欄及內(nèi)容;下方是導(dǎo)航欄的內(nèi)容section。

導(dǎo)航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 id='tab'> <!-- tab標(biāo)簽 --> <nav class='firstnav'><ul> <li class='liactive'><span>測(cè)試1</span><span class='icon-guanbi'>X</span></li> <li><span>測(cè)試2</span><span class='icon-guanbi'>X</span></li> <li><span>測(cè)試3</span><span class=' icon-guanbi'>X</span></li></ul><div class='tabadd'> <span>+</span></div> </nav> <!-- tab內(nèi)容 --> <div class='tabscon'><section class='conactive'>測(cè)試內(nèi)容1</section><section>測(cè)試內(nèi)容2</section><section>測(cè)試內(nèi)容3</section> </div> </div> </main> <script src='http://www.aoyou183.cn/bcjs/js/tabs.js'></script></body></html><style> * { margin: 0; padding: 0;}ul li { list-style: none;}main { width: 960px; height: 500px; border-radius: 10px; margin: 50px auto;}main h4 { height: 100px; line-height: 100px; text-align: center;}.tabsbox { width: 900px; margin: 0 auto; height: 400px; border: 1px solid lightsalmon; position: relative;}nav ul { overflow: hidden;}nav ul li { float: left; width: 100px; height: 50px; line-height: 50px; text-align: center; border-right: 1px solid #ccc; position: relative;}nav ul li.liactive { border-bottom: 2px solid #fff; z-index: 9;}#tab input { width: 80%; height: 60%;}nav ul li span:last-child { position: absolute; user-select: none; font-size: 12px; top: -18px; right: 0; display: inline-block; height: 20px;}.tabadd { position: absolute; /* width: 100px; */ top: 0; right: 0;}.tabadd span { display: block; width: 20px; height: 20px; line-height: 20px; text-align: center; border: 1px solid #ccc; float: right; margin: 10px; user-select: none;}.tabscon { width: 100%; height: 300px; position: absolute; padding: 30px; top: 50px; left: 0px; box-sizing: border-box; border-top: 1px solid #ccc;}.tabscon section,.tabscon section.conactive { display: none; width: 100%; height: 100%;}.tabscon section.conactive { display: block;}</style>

由于給導(dǎo)航欄增刪查改的js代碼很多,我單獨(dú)用一個(gè)js文件寫tabs.js,在tabs.html里引用就行。

實(shí)現(xiàn)的導(dǎo)航欄功能有:

1)導(dǎo)航欄的切換功能

2)增加導(dǎo)航欄的功能

3)刪除導(dǎo)航欄的功能

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>新選項(xiàng)卡</span><span class='iconfont icon-guanbi'>X</span></li>’; var section = ’<section class='conactive'>’+random+’</section>’; //2)把兩個(gè)元素添加到對(duì)應(yīng)的父級(jí)元素中 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); //刪除對(duì)應(yīng)節(jié)點(diǎn) that.lis[index].remove(); that.sections[index].remove(); //刪除后及時(shí)更新頁面中的節(jié)點(diǎn) that.init(); //如果當(dāng)前頁面有選中狀態(tài),就不用執(zhí)行下面的步驟 if(document.querySelector(’.liactive’)) return; //讓index前一個(gè)處于選中狀態(tài) index--; that.lis[index] && that.lis[index].click(); } //4.修改功能 editTab(){ let str = this.innerHTML; // console.log(str); //禁止雙擊選中文字 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); this.innerHTML=’<input type='text'>’ let input = this.children[0]; input.value = str; input.select(); //input失去焦點(diǎn)后變回span input.onblur = function(){ this.parentNode.innerHTML= this.value; } //按回車也能 input.onkeyup = function(e){ if(e.keyCode == 13){ this.blur();//獲得焦點(diǎn) } } }} new Tab(’#tab’);// tab.init();

到此這篇關(guān)于原生JavaScript寫出Tabs標(biāo)簽頁的文章就介紹到這了,更多相關(guān)js tabs 標(biāo)簽頁內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 国产精品喷水在线观看 | 亚洲最大看欧美片网站 | 香蕉在线精品视频在线观看2 | 免费在线色视频 | 午夜影皖普通区 | 国产伦精品一区二区三区女 | 91人人视频国产香蕉 | 51精品| 免费的黄色小视频 | 五月久久噜噜噜色影 | 新香蕉视频在线 | 91av爱爱| 在线久综合色手机在线播放 | 国产成人精品女人不卡在线 | 香蕉在线精品视频在线观看2 | 亚洲一级毛片在线观 | 成人网久久 | 精品日韩在线观看 | 综合另类 | 婷婷六月丁香色婷婷网 | 亚洲国产欧美国产第一区 | 亚洲久久久久 | 自偷自拍亚洲欧美清纯唯美 | 婷婷亚洲五月色综合 | 黄色网在线免费观看 | 国产亚洲精品国产 | 欧美一级特黄刺激爽大片 | a级黄色网 | 成人黄网大全在线观看 | 国产真实女人一级毛片 | 香蕉视频在线免费看 | 高清欧美在线三级视频 | 免费播放欧美毛片欧美a | 亚洲免费精品 | 中文字幕成人 | 深夜偷偷看视频在线观看 | 国产一区二区三区四区偷看 | 欧美日韩精品在线观看 | 国产在线精品二区韩国演艺界 | 欧美a级成人淫片免费看 | 国产精品片 |