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

4種JavaScript實現(xiàn)簡單tab選項卡切換的方法

 更新時間:2016年01月06日 09:15:17   作者:Cakty、Riven  
這篇文章主要介紹了4種JavaScript實現(xiàn)簡單tab選項卡切換的方法,感興趣的小伙伴們可以參考一下

本文實例講解了4種JavaScript實現(xiàn)簡單tab選項卡切換的方法,分享給大家供大家參考,具體內容如下
效果圖:

 

方法一:for循環(huán)+if判斷當前點擊與自定義數(shù)組是否匹配

<!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">
 //定義數(shù)組并獲取數(shù)組內各個的節(jié)點
 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循環(huán)遍歷button數(shù)組長度
  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";
  //判斷當前點擊是按鈕數(shù)組中的哪一個?
  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+匿名函數(shù)的自執(zhí)行

<!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){ //匿名函數(shù)自執(zhí)行
  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選項卡操作方法匯總 jquery選項卡操作方法匯總

希望本文所述對大家學習javascript程序設計有所幫助。

相關文章

  • 基于javascript實現(xiàn)的快速排序

    基于javascript實現(xiàn)的快速排序

    本篇文章主要介紹了javascript實現(xiàn)的快速排序的方法與原理說明:找基準點、建立二個數(shù)組分別存儲、遞歸。需要的朋友來看下吧
    2016-12-12
  • JS判斷是否長按某一鍵的方法

    JS判斷是否長按某一鍵的方法

    這篇文章主要介紹了JS判斷是否長按某一鍵的方法,涉及JavaScript針對鍵盤按鍵的判斷及響應操作技巧,需要的朋友可以參考下
    2016-03-03
  • javascript 彈出層居中效果的制作

    javascript 彈出層居中效果的制作

    做一個帶拖動的彈出層效果(像提示框那種) ,看了下代碼與實現(xiàn)效果,值得學習應用。
    2009-09-09
  • JS實現(xiàn)一個秒表計時器

    JS實現(xiàn)一個秒表計時器

    這篇文章主要為大家詳細介紹了JS實現(xiàn)一個秒表計時器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 微信jssdk用法匯總

    微信jssdk用法匯總

    這篇文章主要針對微信jssdk用法進行匯總,通過ready接口處理成功驗證、通過error接口處理失敗驗證等內容介紹,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 你不知道的5個JavaScript中JSON的秘密功能分享

    你不知道的5個JavaScript中JSON的秘密功能分享

    在開發(fā)中,我們會經(jīng)常使用?JSON.stringify(object)?來序列化對象,但JSON.stringify方法除了了第一個參數(shù)外,還有其它參數(shù)可用,今天我們一起來看看這些參數(shù)是做啥的
    2023-05-05
  • js如何讀取csv內容拼接成json

    js如何讀取csv內容拼接成json

    這篇文章主要介紹了js如何讀取csv內容拼接成json,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • JavaScript中this函數(shù)使用實例解析

    JavaScript中this函數(shù)使用實例解析

    這篇文章主要介紹了JavaScript中this函數(shù)使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • use jscript with List Proxy Server Information

    use jscript with List Proxy Server Information

    use jscript with List Proxy Server Information...
    2007-06-06
  • 微信小程序scroll-view實現(xiàn)滾動穿透和阻止?jié)L動的方法

    微信小程序scroll-view實現(xiàn)滾動穿透和阻止?jié)L動的方法

    這篇文章主要介紹了微信小程序scroll-view實現(xiàn)滾動穿透和阻止?jié)L動的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論

黄石市| 县级市| 祁门县| 融水| 将乐县| 稷山县| 江华| 保康县| 宿迁市| 什邡市| 中山市| 白河县| 东台市| 赤城县| 正定县| 隆化县| 平度市| 屯门区| 武义县| 庆云县| 德化县| 青阳县| 老河口市| 白水县| 桐城市| 龙州县| 渝中区| 灵川县| 福海县| 怀化市| 台湾省| 东乌珠穆沁旗| 镇康县| 镇康县| 仪征市| 昌黎县| 长汀县| 广汉市| 富蕴县| 彭泽县| 虹口区|