100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 4种JavaScript实现简单tab选项卡切换办法【javascript】

4种JavaScript实现简单tab选项卡切换办法【javascript】

时间:2019-09-28 09:10:47

相关推荐

4种JavaScript实现简单tab选项卡切换办法【javascript】

web前端|js教程

JavaScript,tab,选项卡

web前端-js教程

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

效果图:

淘宝api网站源码,vscode 等宽设置,ubuntu查看进程内容,被tomcat 404,sqlite and语句,wordpress 404 插件,前端图形用什么框架,爬虫接单论坛,PHP教程下载视频,SEO优化关键词尤克里,汽修管理网站源码,网页聊天室网站,discuz新媒体模板,门户页面html,全套商城网站后台管理系统网站管理页面,网站程序是模版吗lzw

算法源码下载论坛,Ubuntu合盖无法启动,tomcat7 转义字符,爬虫珍宝堂,php 监控网页超时,Seo团队组建_Seo团队lzw

方法一:for循环+if判断当前点击与自定义数组是否匹配

云 java源码,node vscode,ubuntu 声音,linux启动tomcat,sqlite 树,网页设计尺寸多少, 阻塞 整站 数据库,组装服务器去卖,响应式轮播图插件,前端快速开发框架,node JS 爬虫,替换 php,seo的排名,springboot接口指令,a标签 href 没反应,源码交易网站,网页设计样本,dz门户模板,discuz 源代码显示的关键词和后台设计的关键词不一样,h5跟随页面下拉的按钮,.net 会员卡管理系统,微信开发小程序lzw

tab切换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; }

1

2

3

4

//定义数组并获取数组内各个的节点 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); //显示点击按钮对应的divdivArr[j].style.display = "block"; }} } }

方法二:自定义index为当前点击

tab切换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; }

1

2

3

4

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";} } }

方法三:className

tab* {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; }

1

2

3

4

//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"; } }

方法四:className+匿名函数的自执行

tab* {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; }

1

2

3

4

//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) }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。