分享幾種比較簡單實(shí)用的JavaScript tabel切換
閑著沒事,隨便寫了個(gè)簡單的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ù)組長度
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)擊按鈕對應(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)容是小編給大家分享幾種比較簡單實(shí)用的JavaScript tabel切換,希望大家喜歡。
- js對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簡單展現(xiàn)table數(shù)據(jù)示例
- 利用js制作html table分頁示例(js實(shí)現(xiàn)分頁)
- 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、對象轉(zhuǎn)換成Json的實(shí)現(xiàn)代碼
- JS動(dòng)態(tài)添加Table的TR,TD實(shí)現(xiàn)方法
- JS獲取Table中td值的方法
相關(guān)文章
JavaScript高階函數(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JavaScript高階函數(shù),詳細(xì)講解了什么是高階函數(shù)和高階函數(shù)的用法,有興趣的可以了解下2017-06-06
JavaScript中l(wèi)ocalStorage對象存儲方式實(shí)例分析
這篇文章主要介紹了JavaScript中l(wèi)ocalStorage對象存儲方式,結(jié)合實(shí)例形式分析了localStorage對象存儲數(shù)據(jù)的原理及操作技巧,需要的朋友可以參考下2017-01-01
學(xué)習(xí)javascript面向?qū)ο?理解javascript對象
這篇文章主要介紹了javascript對象,學(xué)習(xí)javascript面向?qū)ο?,感興趣的小伙伴們可以參考一下2016-01-01
詳解JavaScript 新語法之Class 的私有屬性與私有方法
這篇文章主要介紹了JavaScript 新語法之Class 的私有屬性與私有方法 ,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Javascript insertAfter() 實(shí)現(xiàn)函數(shù)代碼
DOM沒有提供insertAfter()方法,我們可以自己擴(kuò)展下。2011-10-10
javascript中的注釋使用與注意事項(xiàng)小結(jié)
在javascript中有兩種注釋方式,單行注釋與多行注釋。2011-09-09

