简单实现js选项卡切换效果
作者:jerrylsxu 时间:2024-05-03 15:05:24
本文实例介绍了实现js选项卡切换效果的详细代码,分享给大家供大家参考,具体内容如下
思路:
1、获取元素;
2、for循环按钮元素添加onclick(点击) 或者 onmousemove(移入)事件;
3、点击当前按钮时会以高亮状态显示,通过for循环历遍把所有的按钮样式设置为空在把所有div的display设置为none。
4、点击当前按钮添加样式,把当前div显示出来,display设置为block。
html代码:
<div id="div1">
<input type="button" class="active" value="1"/>
<input type="button" value="2"/>
<input type="button" value="3"/>
<input type="button" value="4"/>
<div class="div2" style="display:block;">11</div>
<div class="div2">22</div>
<div class="div2">33</div>
<div class="div2">44</div>
</div>
css样式:
.active
{
background:#9CC;
}
.div2
{
width:300px;
height:200px;
border:1px #666666 solid;
display:none;
}
js代码:
<script>
window.onload=function(){
var odiv=document.getElementById('div1');//获取div
var btn=odiv.getElementsByTagName('input');//获取div下的input
var div2=odiv.getElementsByTagName('div') ;//获取div下的div
for(i=0;i<btn.length;i++)//循环每个按钮
{
btn[i].index=i //btn[i]是指定button的第i个按钮;为该按钮添加一个index属性,并将index的值设置为i
btn[i].onclick=function()//按钮的第i个点击事件
{
for(i=0;i<btn.length;i++)//循环去掉button的样式,把div隐藏
{
btn[i].className='' //清空按钮的样式
div2[i].style.display='none'//隐藏div
}
this.className='active'//自身添加active
div2[this.index].style.display='block'//this.index是当前div
}
}
}
</script>
标签:js,选项卡
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python数据库的连接实现方法与注意事项
2024-01-14 10:51:54
用Python shell简化开发
2023-12-16 17:19:04
![](https://img.aspxhome.com/file/2023/6/117856_0s.jpg)
使用python对多个txt文件中的数据进行筛选的方法
2022-02-19 10:09:30
![](https://img.aspxhome.com/file/2023/5/110545_0s.jpg)
mysql data文件夹位置查找
2024-01-15 08:20:38
利用Pandas 创建空的DataFrame方法
2022-07-11 17:25:05
Python字节单位转换(将字节转换为K M G T)
2023-11-04 02:26:18
IE7兼容模式与兼容视图
2010-06-28 18:48:00
Python实现统计给定列表中指定数字出现次数的方法
2023-02-04 09:50:29
![](https://img.aspxhome.com/file/2023/7/134547_0s.png)
MySQL在右表数据不唯一的情况下使用left join的方法
2024-01-18 22:42:02
![](https://img.aspxhome.com/file/2023/0/114020_0s.png)
codeigniter发送邮件并打印调试信息的方法
2024-05-13 09:57:03
Python实现mysql数据库更新表数据接口的功能
2024-01-18 01:07:43
matlab中乘法“*”和点乘“.*”;除法“/”和点除“./”的联系和区别
2022-03-08 19:52:44
python实现Nao机器人的单目测距
2021-04-09 16:37:10
基于Python绘制世界疫情地图详解
2021-11-23 09:10:36
![](https://img.aspxhome.com/file/2023/3/133233_0s.jpg)
零基础写python爬虫之抓取百度贴吧代码分享
2022-09-04 23:55:12
python直接获取API传递回来的参数方法
2023-11-10 18:01:59
python+requests+pytest接口自动化的实现示例
2022-11-01 06:12:27
![](https://img.aspxhome.com/file/2023/2/64302_0s.jpg)
在matlab中创建类似字典的数据结构方式
2021-07-08 01:22:53
![](https://img.aspxhome.com/file/2023/5/107475_0s.jpg)
解决iframe的frameborder在chrome/ff/ie下的差异
2023-08-09 00:23:24
利用Python实现命令行版的火车票查看器
2021-10-24 13:59:21