分享幾種比較簡(jiǎn)單實(shí)用的JavaScript tabel切換
閑著沒事,隨便寫了個(gè)簡(jiǎn)單的JavaScript tabel切換,大家有興趣的看看,有需要的就拿去吧.廢話不說了,大家看代碼吧
方法一: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>
以上內(nèi)容是小編給大家分享幾種比較簡(jiǎn)單實(shí)用的JavaScript tabel切換,希望大家喜歡。
- js對(duì)table的td進(jìn)行相同內(nèi)容合并示例詳解
- js無刷新操作table的行和列
- Js實(shí)現(xiàn)動(dòng)態(tài)添加刪除Table行示例
- C#中把Datatable轉(zhuǎn)換為Json的5個(gè)代碼實(shí)例
- jquery easyui 結(jié)合jsp簡(jiǎn)單展現(xiàn)table數(shù)據(jù)示例
- 利用js制作html table分頁(yè)示例(js實(shí)現(xiàn)分頁(yè))
- JavaScript獲取table中某一列的值的方法
- JQuery實(shí)現(xiàn)table行折疊效果以JSON做數(shù)據(jù)源
- 通過Jquery的Ajax方法讀取將table轉(zhuǎn)換為Json
- JS使用for循環(huán)遍歷Table的所有單元格內(nèi)容
- C#中的DataSet、string、DataTable、對(duì)象轉(zhuǎn)換成Json的實(shí)現(xiàn)代碼
- JS動(dòng)態(tài)添加Table的TR,TD實(shí)現(xiàn)方法
- JS獲取Table中td值的方法
相關(guān)文章
js循環(huán)動(dòng)態(tài)綁定帶參數(shù)函數(shù)遇到的問題及解決方案[轉(zhuǎn)]
關(guān)于Javascript利用循環(huán)綁定事件的例子,需要的朋友可以參考下。2010-11-11JavaScript實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的盒子
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的盒子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01七個(gè)基于JavaScript實(shí)現(xiàn)的情人節(jié)表白特效
情人節(jié)將至 程序員證明自己不是直男的時(shí)候到啦 我們也有自己的專屬代碼浪漫。本文將介紹七個(gè)利用JavaScript實(shí)現(xiàn)的情人節(jié)表白特效,需要的可以參考一下2022-01-01JavaScript設(shè)計(jì)模式之裝飾者模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之裝飾者模式介紹,通一個(gè)類來動(dòng)態(tài)的對(duì)另一個(gè)類的功能對(duì)象進(jìn)行前或后的修飾,給它輔加一些額外的功能; 這是對(duì)一個(gè)類對(duì)象功能的裝飾,裝飾的類跟被裝飾的類,要求擁有相同的訪問接口方法(功能),需要的朋友可以參考下2014-12-12原生js實(shí)現(xiàn)簡(jiǎn)易抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)簡(jiǎn)易抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03