4种JavaScript实现简单tab选项卡切换的方法


Posted in Javascript onJanuary 06, 2016

本文实例讲解了4种JavaScript实现简单tab选项卡切换的方法,分享给大家供大家参考,具体内容如下
效果图:

 4种JavaScript实现简单tab选项卡切换的方法

方法一: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选项卡操作方法汇总 jquery选项卡操作方法汇总

希望本文所述对大家学习javascript程序设计有所帮助。

Javascript 相关文章推荐
jQuery 性能优化指南(3)
May 21 Javascript
JavaScript 大数据相加的问题
Aug 03 Javascript
THREE.JS入门教程(5)你应当知道的十件事
Jan 24 Javascript
javaScript NameSpace 简单说明介绍
Jul 18 Javascript
详解WordPress开发中get_current_screen()函数的使用
Jan 11 Javascript
JavaScrip关于创建常量的知识点
Dec 07 Javascript
Vue官网todoMVC示例代码
Jan 29 Javascript
Vue三层嵌套路由的示例代码
May 05 Javascript
create-react-app安装出错问题解决方法
Sep 04 Javascript
微信小程序实现自动定位功能
Oct 31 Javascript
layDate日期控件使用方法详解
Nov 15 Javascript
CKeditor富文本编辑器使用技巧之添加自定义插件的方法
Jun 14 Javascript
js针对ip地址、子网掩码、网关的逻辑性判断
Jan 06 #Javascript
js判断当前页面在移动设备还是在PC端中打开
Jan 06 #Javascript
js判断当前页面用什么浏览器打开的方法
Jan 06 #Javascript
javascript中闭包(Closure)详解
Jan 06 #Javascript
果断收藏9个Javascript代码高亮脚本
Jan 06 #Javascript
基于HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
Jan 05 #Javascript
JavaScript实现简单的tab选项卡切换
Jan 05 #Javascript
You might like
收听短波不可能有声音清晰的品质吗
2021/03/01 无线电
用PHP生成html分页列表的代码
2007/03/18 PHP
PHP写MySQL数据 实现代码
2009/06/15 PHP
解析PHP高效率写法(详解原因)
2013/06/20 PHP
处理(php-cgi.exe - FastCGI 进程超过了配置的请求超时时限)的问题
2013/07/03 PHP
table标签的结构与合并单元格的实现方法
2013/07/24 PHP
php 判断页面或图片是否经过gzip压缩的方法
2017/04/05 PHP
PHP读取文件,解决中文乱码UTF-8的方法分析
2020/01/22 PHP
10个实用的脚本代码工具
2010/05/04 Javascript
JavaScript高级程序设计(第3版)学习笔记12 js正则表达式
2012/10/11 Javascript
JavaScript获取和设置CheckBox状态的简单方法
2013/07/05 Javascript
jquery中post方法用法实例
2014/10/21 Javascript
Javascript 是你的高阶函数(高级应用)
2015/06/15 Javascript
JS实现网页标题随机显示名人名言的方法
2015/11/03 Javascript
对javascript继承的理解
2016/10/11 Javascript
关于vue.js v-bind 的一些理解和思考
2017/06/06 Javascript
node.js中cluster的使用教程
2017/06/09 Javascript
解决JS内存泄露之js对象和dom对象互相引用问题
2017/06/25 Javascript
Vuex利用state保存新闻数据实例
2017/06/28 Javascript
小程序自定义日历效果
2018/12/29 Javascript
性能优化篇之Webpack构建速度优化的建议
2019/04/03 Javascript
vue2 中二级路由高亮问题及配置方法
2019/06/10 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
9个JavaScript日常开发小技巧
2020/10/06 Javascript
[03:14]辉夜杯主赛事 12月25日每日之星
2015/12/26 DOTA
Python二分法搜索算法实例分析
2015/05/11 Python
皇家阿尔伯特瓷器美国官网:Royal Albert美国
2020/02/16 全球购物
中医药大学市场营销专业自荐信
2013/09/29 职场文书
开办化妆品公司创业计划书
2013/12/26 职场文书
初中升旗仪式演讲稿
2014/05/08 职场文书
竞选班长演讲稿500字
2014/08/22 职场文书
合作协议书范本
2014/10/25 职场文书
2014年幼儿园老师工作总结
2014/12/05 职场文书
入党积极分子考察意见
2015/06/02 职场文书
最美乡村教师观后感
2015/06/11 职场文书
使用nginx动态转换图片大小生成缩略图
2021/03/31 Servers