最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年07月20日 15:51:57   作者:davidbeckham  
這篇文章主要介紹了原生JavaScript寫出Tabs標(biāo)簽頁(yè)的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

話不多說(shuō),直接開(kāi)始。

首先,是前端頁(yè)面。

圖1 tabs

先來(lái)把tabs分解一下:

圖2 tabs分解

首先,一個(gè)大的框div,上面紅色的框是導(dǎo)航欄nav,導(dǎo)航欄里是一個(gè)無(wú)序列表ul,里面三個(gè)li標(biāo)簽(黃色的框),li標(biāo)簽里兩個(gè)綠色標(biāo)簽是兩個(gè)span,一個(gè)用來(lái)放導(dǎo)航的名字,一個(gè)用來(lái)放導(dǎo)航關(guān)閉的icon,右邊是一個(gè)button,用來(lái)添加新的導(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 class="tabsbox" 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="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í)更新頁(yè)面中的節(jié)點(diǎn)
    that.init();
    //如果當(dāng)前頁(yè)面有選中狀態(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)簽頁(yè)的文章就介紹到這了,更多相關(guān)js tabs 標(biāo)簽頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解uni-app中的樣式

    詳解uni-app中的樣式

    這篇文章主要為大家介紹了uni-app中的樣式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • boostrapTable的refresh和refreshOptions區(qū)別淺析

    boostrapTable的refresh和refreshOptions區(qū)別淺析

    在使用bootstrapTable時(shí),刷新數(shù)據(jù)有兩個(gè)方法refresh、refreshOptions,在其用法上有點(diǎn)區(qū)別,接下來(lái)通過(guò)本文給大家分享boostrapTable的refresh和refreshOptions的區(qū)別,需要的朋友可以參考下
    2017-01-01
  • javascript實(shí)現(xiàn)瀑布流加載圖片原理

    javascript實(shí)現(xiàn)瀑布流加載圖片原理

    這篇文章主要為大家介紹了javascript實(shí)現(xiàn)瀑布流加載圖片效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • element-ui的回調(diào)函數(shù)Events的用法詳解

    element-ui的回調(diào)函數(shù)Events的用法詳解

    這篇文章主要介紹了element-ui的回調(diào)函數(shù)Events的用法,本文通過(guò)實(shí)例代碼給大家介紹了change回調(diào)函數(shù)的使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • js腳本編寫簡(jiǎn)單刷票投票系統(tǒng)

    js腳本編寫簡(jiǎn)單刷票投票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了js腳本編寫簡(jiǎn)單刷票投票系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • JS隱藏參數(shù)post傳值實(shí)例

    JS隱藏參數(shù)post傳值實(shí)例

    JS隱藏參數(shù)post傳值實(shí)例,需要的朋友可以參考一下
    2013-04-04
  • JS數(shù)組降維的幾種方法詳解

    JS數(shù)組降維的幾種方法詳解

    這篇文章主要介紹了JS數(shù)組降維的幾種方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • js實(shí)現(xiàn)的點(diǎn)擊div區(qū)域外隱藏div區(qū)域

    js實(shí)現(xiàn)的點(diǎn)擊div區(qū)域外隱藏div區(qū)域

    這篇文章主要介紹了通過(guò)js實(shí)現(xiàn)點(diǎn)擊div區(qū)域外隱藏div區(qū)域,原理及示例代碼如下
    2014-06-06
  • document.all還是document.getElementsByName?

    document.all還是document.getElementsByName?

    document.all還是document.getElementsByName?...
    2006-07-07
  • js實(shí)現(xiàn)自動(dòng)鎖屏功能

    js實(shí)現(xiàn)自動(dòng)鎖屏功能

    有這么一個(gè)需求,開(kāi)發(fā)了一套系統(tǒng),當(dāng)用戶離開(kāi)桌面或者一段時(shí)間不操作的話,需要把該系統(tǒng)所有打開(kāi)頁(yè)面鎖定起來(lái),本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-06-06

最新評(píng)論

桐梓县| 措美县| 新疆| 南京市| 大悟县| 亚东县| 兴文县| 离岛区| 长治县| 武邑县| 锡林浩特市| 东安县| 原阳县| 锦州市| 广德县| 望奎县| 永清县| 安国市| 广灵县| 靖边县| 福泉市| 甘孜| 富川| 湘乡市| 四会市| 两当县| 郑州市| 虞城县| 阿合奇县| 武强县| 霍州市| 崇信县| 微山县| 滦南县| 宜兴市| 沂水县| 新丰县| 阜新市| 南通市| 贵德县| 漳州市|