国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

分享幾種比較簡單實用的JavaScript tabel切換

2019-11-20 10:53:11
字體:
來源:轉載
供稿:網友

閑著沒事,隨便寫了個簡單的JavaScript tabel切換,大家有興趣的看看,有需要的就拿去吧.廢話不說了,大家看代碼吧

方法一:for循環+if判斷當前點擊與自定義數組是否匹配

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>tab切換</title> <style type="text/css">  button {   width:120px;   height: 32px;   line-height: 32px;   background-color: #ccc;   font-size: 24px;  }  div {   display: none;   width:200px;   height: 200px;   font-size: 72px;   color:#ddd;   background-color: green;   border:1px solid black;  } </style></head><body> <button style="background-color: yellow;">1</button> <button>2</button> <button>3</button> <button>4</button> <div style="display:block;">1</div> <div>2</div> <div>3</div> <div>4</div> <script type="text/javascript"> //定義數組并獲取數組內各個的節點 var buttonArr = document.getElementsByTagName("button"); var divArr = document.getElementsByTagName("div"); for(var i = 0; i < buttonArr.length;i++) {  buttonArr[i].onclick = function() {   //this    // alert(this.innerHTML)   //for循環遍歷button數組長度   for(var j = 0; j < buttonArr.length; j++) {    //重置所有的button樣式    buttonArr[j].style.backgroundColor = "#ccc";    //給當前的(點擊的那個)那個button添加樣式    this.style.backgroundColor = "yellow";    //隱藏所有的div    divArr[j].style.display = "none";    //判斷當前點擊是按鈕數組中的哪一個?    if(this == buttonArr[j]) {     // alert(j);      //顯示點擊按鈕對應的div     divArr[j].style.display = "block";    }   }  } } </script></body></html> 

方法二:自定義index為當前點擊

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>tab切換</title> <style type="text/css">  button {   width:120px;   height: 32px;   line-height: 32px;   background-color: #ccc;   font-size: 24px;  }  div {   display: none;   width:200px;   height: 200px;   font-size: 72px;   color:#ddd;   background-color: green;   border:1px solid black;  } </style></head><body> <button style="background-color: yellow;">1</button> <button>2</button> <button>3</button> <button>4</button> <div style="display:block;">1</div> <div>2</div> <div>3</div> <div>4</div> <script type="text/javascript"> var buttonArr = document.getElementsByTagName("button"); var divArr = document.getElementsByTagName("div"); for(var i = 0; i < buttonArr.length;i++) {  buttonArr[i].index = i;  // buttonArr[i].setAttribute("index",i);  buttonArr[i].onclick = function() {   for(var j = 0; j < buttonArr.length; j++) {    buttonArr[j].style.backgroundColor = "#ccc";    buttonArr[this.index].style.backgroundColor = "yellow";    divArr[j].style.display = "none";    divArr[this.index].style.display = "block";   }  } } </script></body></html> 

  方法三:className

<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>tab</title> <style type="text/css">  * {padding:0; margin:0;}  button {   background-color: #ccc;   width:80px;   height: 30px;  }  .btn-active {   background-color: yellow;   font-weight:bold;   font-size: 14px;  }  div{   width:200px;   height: 200px;   font-size: 64px;   background-color: #0c0;   display: none;   color:#fff;  }  .div-active {   display: block;  } </style></head><body> <button class="btn-active">按鈕1</button> <button>按鈕2</button> <button>按鈕3</button> <button>按鈕4</button> <div class="div-active">1</div> <div>2</div> <div>3</div> <div>4</div> <script type="text/javascript"> //1.先獲取元素 var buttonList = document.getElementsByTagName("button"); var divList = document.getElementsByTagName("div"); //2.添加事件 for(var i = 0; i < buttonList.length; i++) {  buttonList[i].index = i;  buttonList[i].onclick = function() {   for(var j = 0; j < buttonList.length;j++) {    buttonList[j].className = "";    divList[j].className = "";   }   this.className = "btn-active";   divList[this.index].className = "div-active";  } } </script></body></html> 

方法四:className+匿名函數的自執行!

<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>tab</title> <style type="text/css">  * {padding:0; margin:0;}  button {   background-color: #ccc;   width:80px;   height: 30px;  }  .btn-active {   background-color: yellow;   font-weight:bold;   font-size: 14px;  }  div{   width:200px;   height: 200px;   font-size: 64px;   background-color: #0c0;   display: none;   color:#fff;  }  .div-active {   display: block;  } </style></head><body> <button class="btn-active">按鈕1</button> <button>按鈕2</button> <button>按鈕3</button> <button>按鈕4</button> <div class="div-active">1</div> <div>2</div> <div>3</div> <div>4</div> <script type="text/javascript"> //1.先獲取元素 var buttonList = document.getElementsByTagName("button"); var divList = document.getElementsByTagName("div"); //2.添加事件 for(var i = 0; i < buttonList.length; i++) {  (function(i){ //匿名函數自執行    buttonList[i].onclick = function() {    for(var j = 0; j < buttonList.length;j++) {     buttonList[j].className = "";     divList[j].className = "";    }    this.className = "btn-active";    divList[i].className = "div-active";   }  })(i) } </script></body></html> 

以上內容是小編給大家分享幾種比較簡單實用的JavaScript tabel切換,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邵东县| 富裕县| 磐石市| 巫山县| 翁牛特旗| 马关县| 定远县| 澳门| 桃源县| 万载县| 莒南县| 孟连| 竹溪县| 都匀市| 怀安县| 马尔康县| 北宁市| 东兴市| 平果县| 临夏市| 霸州市| 晋城| 白河县| 石河子市| 泊头市| 定结县| 泸西县| 平舆县| 富阳市| 红河县| 阜新市| 绿春县| 招远市| 鲜城| 平和县| 沛县| 靖西县| 寿光市| 林甸县| 休宁县| 萝北县|