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

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

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

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

 

方法一:for循環(huán)+if判斷當(dāng)前點(diǎn)擊與自定義數(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ù)組內(nèi)各個(gè)的節(jié)點(diǎn)
 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ù)組長(zhǎng)度
  for(var j = 0; j < buttonArr.length; j++) {
  //重置所有的button樣式
  buttonArr[j].style.backgroundColor = "#ccc";
  //給當(dāng)前的(點(diǎn)擊的那個(gè))那個(gè)button添加樣式
  this.style.backgroundColor = "yellow";
  //隱藏所有的div
  divArr[j].style.display = "none";
  //判斷當(dāng)前點(diǎn)擊是按鈕數(shù)組中的哪一個(gè)?
  if(this == buttonArr[j]) {
   // alert(j);
   //顯示點(diǎn)擊按鈕對(duì)應(yīng)的div
   divArr[j].style.display = "block";
  }
  }
 }
 }
 </script>
</body>
</html>

方法二:自定義index為當(dāng)前點(diǎn)擊

<!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>

如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:javascript選項(xiàng)卡操作方法匯總 jquery選項(xiàng)卡操作方法匯總

希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

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

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

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

    JS判斷是否長(zhǎng)按某一鍵的方法

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

    javascript 彈出層居中效果的制作

    做一個(gè)帶拖動(dòng)的彈出層效果(像提示框那種) ,看了下代碼與實(shí)現(xiàn)效果,值得學(xué)習(xí)應(yīng)用。
    2009-09-09
  • JS實(shí)現(xiàn)一個(gè)秒表計(jì)時(shí)器

    JS實(shí)現(xiàn)一個(gè)秒表計(jì)時(shí)器

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

    微信jssdk用法匯總

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

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

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

    js如何讀取csv內(nèi)容拼接成json

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

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

    這篇文章主要介紹了JavaScript中this函數(shù)使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    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實(shí)現(xiàn)滾動(dòng)穿透和阻止?jié)L動(dòng)的方法

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

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

最新評(píng)論

论坛| 隆尧县| 武平县| 康乐县| 连平县| 买车| 宝坻区| 商丘市| 五莲县| 天台县| 文成县| 永兴县| 福安市| 白玉县| 四平市| 阿拉尔市| 资阳市| 象山县| 宝坻区| 凌源市| 广宁县| 边坝县| 延边| 鄂托克旗| 嵩明县| 大石桥市| 同德县| 邹城市| 平陆县| 丽水市| 中卫市| 新宁县| 乐平市| 若羌县| 塘沽区| 天水市| 驻马店市| 北安市| 浦北县| 中方县| 衡水市|