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 HotKeys轻松搞定键盘事件代码
Aug 30 Javascript
javascript showModalDialog,open取得父窗口的方法
Mar 10 Javascript
使用CDN和AJAX加速WordPress中jQuery的加载
Dec 05 Javascript
第七篇Bootstrap表单布局实例代码详解(三种表单布局)
Jun 21 Javascript
js仿微信语音播放实现思路
Dec 12 Javascript
vue组件父子间通信之综合练习(聊天室)
Nov 07 Javascript
bootstrap table支持高度百分比的实例代码
Feb 28 Javascript
vue自定义全局共用函数详解
Sep 18 Javascript
爬虫利器Puppeteer实战
Jan 09 Javascript
vue-router结合vuex实现用户权限控制功能
Nov 14 Javascript
Vue项目前后端联调(使用proxyTable实现跨域方式)
Jul 18 Javascript
JavaScript 判断数据类型的4种方法
Sep 11 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
php 深入理解strtotime函数的使用详解
2013/05/23 PHP
yii框架结合charjs统计上一年与当前年数据的方法示例
2020/04/04 PHP
经典的解除许多网站无法复制文字的绝招
2006/12/31 Javascript
用jscript启动sqlserver
2007/06/21 Javascript
理解JAVASCRIPT中hasOwnProperty()的作用
2013/06/05 Javascript
JS将表单导出成EXCEL的实例代码
2013/11/11 Javascript
javascript从右边截取指定字符串的三种实现方法
2013/11/29 Javascript
JS对象与json字符串格式转换实例
2014/10/28 Javascript
jQuery制作拼图小游戏
2015/01/12 Javascript
Bootstrap基本模板的使用和理解1
2016/12/14 Javascript
js仿淘宝评价评分功能
2017/02/28 Javascript
bootstrap时间插件daterangepicker使用详解
2017/10/19 Javascript
Vue实现按钮旋转和移动位置的实例代码
2018/08/09 Javascript
Layui实现数据表格中鼠标悬浮图片放大效果,离开时恢复原图的方法
2019/09/11 Javascript
Vue实现商品飞入购物车效果(电商项目)
2019/11/26 Javascript
JS函数进阶之继承用法实例分析
2020/01/15 Javascript
python实现基于两张图片生成圆角图标效果的方法
2015/03/26 Python
Python中正则表达式详解
2017/05/17 Python
Python基于回溯法子集树模板解决m着色问题示例
2017/09/07 Python
对pandas写入读取h5文件的方法详解
2018/12/28 Python
python实现学员管理系统
2019/02/26 Python
Python实现直方图均衡基本原理解析
2019/08/08 Python
python 爬虫之selenium可视化爬虫的实现
2020/12/04 Python
css3制作动态进度条以及附加jQuery百分比数字显示
2012/12/13 HTML / CSS
HTML 5 标签、属性、事件及浏览器兼容性速查表 附打包下载
2012/10/20 HTML / CSS
html5中svg canvas和图片之间相互转化思路代码
2014/01/24 HTML / CSS
利用Storage Event实现页面间通信的示例代码
2018/07/26 HTML / CSS
北大自主招生自荐信
2013/10/19 职场文书
行政文员实习自我鉴定范文
2014/09/14 职场文书
党的群众路线个人对照检查材料
2014/09/23 职场文书
学校政风行风整改方案
2014/10/25 职场文书
生活委员竞选稿
2015/11/21 职场文书
庭外和解协议书
2016/03/23 职场文书
使用python向MongoDB插入时间字段的操作
2021/05/18 Python
Mongo服务重启异常问题的处理方法
2021/07/01 MongoDB
MySQL系列之十五 MySQL常用配置和性能压力测试
2021/07/02 MySQL